How to Manage WordPress from R

Deepanshu Bhalla Add Comment

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.

WordPress API Integration in R

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.

Table of Contents

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 needs 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 <- "https://www.example.com"
wp_username <- "xxxxxxxxxxxxxxxxxx"
wp_password <- "xxxxxxxxxxxxxxxxxx"  # Application_Password

# Create the authorization token
wp_credentials <- paste0(wp_username, ":", wp_password)
wp_token  <-  base64encode(charToRaw(wp_credentials))
wp_header <-  add_headers(Authorization = paste("Basic", wp_token))

Get WordPress Post or Page Content

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


all_posts <- get_content(wp_site = wp_site,
                         wp_header = wp_header,
                         content_type = "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 <- 360
page_data <- get_content(page_id,
                            wp_site,
                            wp_header = wp_header,
                            content_type = "posts")

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 <- get_content_pagination(wp_site, wp_header = wp_header)

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


all_pages <- get_content(wp_site = wp_site,
                         wp_header = wp_header,
                         content_type = "pages")

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


all_pages2 <- get_content_pagination(wp_site,
                                     wp_header = wp_header,
                                     content_type = "pages")

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


page_id <- 699
page_data <- get_content(page_id,
                            wp_site,
                            wp_header = wp_header,
                            content_type = "pages")

Create a New WordPress Post or Page

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


new_post <- list(
  title = "New Post Title",
  content = "This is the content of the new post.",
  slug = "new-post-2024",
  status = "publish"
)

created_post <- post_content(wp_site = wp_site,
                             wp_header = wp_header,
                             data = new_post,
                             content_type = "posts")
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 <- list(
  title = "New Page Title",
  content = "This is the content of the new page.",
  slug = "new-page-2024",
  status = "publish"
)

created_page <- post_content(wp_site = wp_site,
                                wp_header = wp_header,
                                data = new_page,
                                content_type = "pages")

Update WordPress Page or Post

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


updated_data <- list(
  title = "Updated Title2",
  content = "This is the updated content 2."
)

page_id <- 210
updated_page <- modify_content(page_id,
                               wp_site,
                               wp_header = wp_header,
                               data = updated_data,
                               content_type = "pages")

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 <- created_post$id
delete_response <- delete_content(page_id,
                                     wp_site,
                                     wp_header = wp_header,
                                     content_type = "posts")

Get Media Items from WordPress

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


media_items <- get_media(wp_site, 
                         wp_header = wp_header,
                         per_page = 30, 
                         page = 1)
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 <- 125
deleted_media <- delete_media(media_id, wp_site, wp_header = wp_header, force = TRUE)

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


for (media_id in media_items$id) {
  deleted_media <- delete_media(media_id,
                                   wp_site,
                                   wp_header = wp_header,
                                   force = TRUE)
}

Upload Image to WordPress Media Library

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


image_path <- "C:/Users/deepa/Downloads/NVDA.png"
uploaded_image <- upload_media(image_path, wp_site, wp_header = wp_header)
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Manage WordPress from R"
Next → ← Prev