Thursday, October 3, 2024
No menu items!
HomeData Analytics and VisualizationHow to Manage WordPress from R

How to Manage WordPress from R

This tutorial explains how to use R to interact with WordPress. WordPress is the most popular blogging platform that allows users to create and manage websites, blogs etc.

I developed an R package named wpressR to work with WordPress API. It provides various functions to perform operations such as creating, extracting, updating and deleting WordPress posts, pages and media items (images) directly from R.

Install WordPress R Package

Run the following code to install the package wpressR direcly from Github.

remotes::install_github(“deepanshu88/wpressR”)

Authentication

The first step is to authenticate your API requests. WordPress uses Basic Authentication to verify user credentials. You will need your WordPress username and an application password or API token to generate a base64-encoded token.

To generate an Application Password for WordPress, log in to your WordPress dashboard, go to Users > Your Profile and then scroll down to the Application Passwords section. Enter a name for the application and click Add New Application Password to generate a password.

library(wpressR)
library(base64enc)

# Set up your WordPress credentials and API endpoint
wp_site

Get WordPress Post or Page Content

This function get_content() pulls a page or post from a WordPress site.

all_posts

Arguments

id (optional) – The ID of the post or page to retrieve. If NULL, all posts/pages are fetched.
wp_site – URL of the WordPress site (e.g. “https://example.com”).
wp_header – Authentication header.
content_type (optional) – The type of content to fetch either “posts” or “pages”. Defaults to “posts”.

You can use the argument id to fetch content of a specific post.

# Specific Post
page_id

By default, the API returns a maximum of 100 posts per page. If you have a large blog with more than 100 posts, you can use the function get_content_pagination to extract all the WordPress posts.

all_posts2

You can set the argument content_type to “pages” to extract content of all the WordPress pages.

all_pages

To extract details of more than 100 pages, use the code below.

all_pages2

To extract information of a specific page, use the following code.

page_id

Create a New WordPress Post or Page

This function post_content() allows you to create a new post on a WordPress site.

new_post

Arguments

wp_site – URL of the WordPress site (e.g. “https://example.com”).
wp_header – Authentication header.
data – A list containing the new post data such as title, content and status.
content_type (optional) – The type of content to create either “posts” or “pages”. Defaults to “posts”.

To create a new page on a WordPress site, use “pages” in the content_type argument.

new_page

Update WordPress Page or Post

This function modify_content() allows you to update an existing page or post on a WordPress site.

updated_data

Delete WordPress Page or Post

The function delete_content() deletes a specific page or post on a WordPress site using the REST API.

page_id

Get Media Items from WordPress

This function get_media() fetches a list of media items (images/videos) from a WordPress site.

media_items

Arguments

wp_site – URL of the WordPress site.
wp_header – Authentication header.
per_page (optional) – Number of media items to retrieve per page. Defaults to 10.
page (optional) – The page number of media items to retrieve. Defaults to 1.

Delete Media Items from WordPress

This function delete_media() deletes a specific media item like image from the WordPress media library.

media_id

To delete all media items, you can loop over ids fetched from get_media() function.

for (media_id in media_items$id) {
deleted_media

Upload Image to WordPress Media Library

This function upload_media() uploads an image from the local drive to the WordPress media library.

image_path

Read MoreListenData

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments