WordPress XML-RPC Client


A Wolfram Language client for the XML-RPC WordPress API.


There are two different ways to provide login information:

SetCredentials[
 username,
 password,
 endpoint,
 blogID(*Optional*)
 ]

SetCredentials[<|
  "username" -> "username",
  "password" -> "password",
  "endpoint" -> "endpoint",
  "blogID" -> "blogID"
  |>]

The advantage with the latter is that all parameters are optional, i.e. it is possible to update just the username or just the password without changing the value of any of the other credentials. blogID is only used for multi-user WordPress networks, otherwise it defaults to 1, which is the blog ID for normal WordPress installs.

Two more functions related to credentials:

GetCredentials[]
(* Out: <|"username" -> "username", "password" -> "password", 
 "endpoint" -> "endpoint", "blogID" -> 1|> *)

ClearCredentials[]

The following is a complete list of all the functions that are provided by the API. Note that there is a one to one correspondence between the package's API and the WordPress XML-RPC API.

Package API

The one to one correspondence is true also for the arguments of all the functions. "structs" are represented in Mathematica by Association whereas arrays are represented by List ({}). Below is a piece of code that generates a word cloud of the Declaration of Independence in a very roundabout manner. It starts by creating a WordPress page with a placeholder text, then it updates that page and replaces the placeholder text with the Declaration of Independence, before it finally retrieves the text from the page back into Mathematica, where the word cloud is generated.

text = ExampleData[{"Text", "DeclarationOfIndependence"}];

(* http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost *)
id = newPost[<|
     "post_type" -> "page",
     "post_title" -> "Declaration of Independence",
     "post_content" -> "placeholder",
     "post_status" -> "publish"
     |>] // ToExpression;

(* http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.editPost *)
editPost[id, <|
   "post_content" -> text
   |>];

(* http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost *)
post = getPost[id];

wordcloud = WordCloud[post["post_content"]]

Word Cloud

The API even has the capability to upload files, but first the file has to be converted into a "Base64" encoded string. The package uses the special head "Base64" to denote such strings. Here is how to upload the word cloud generated in the previous example and retrieve the images URL:

Export["~/Desktop/wordcloud.png", wordcloud];
data = "Base64"@
   ExportString[Import["~/Desktop/wordcloud.png", "String"], "Base64"];

(* http://codex.wordpress.org/XML-RPC_WordPress_API/Media#wp.uploadFile *)
file = uploadFile[<|
    "name" -> "wordcloud.png",
    "type" -> "image/png",
    "bits" -> data
    |>];

file["url"]
(* Out: http://domain.tld/wp-content/uploads/2015/09/wordcloud.png *)

Last but not least, all API functions support the concept of callbacks. What this means is that after an API function has been evaluated, whether successfully or not, each function defined as a callback by using AddCallback will be evaluated. Its argument will be either of the following:

error[<|
    "event" -> "response",
    "credentials" -> GetCredentials[],
    "method" -> method,
    "params" -> params,
    "responsexml" -> responsexml
    |>];

success[<|
    "event" -> "response",
    "credentials" -> GetCredentials[],
    "method" -> method,
    "params" -> params,
    "responsexml" -> responsexml
    |>];

That is, the head of the argument given to callbacks can be used to determine whether the API call was completed successfully or not. method is a string that corresponds to the name of the API function with wp. prepended. Event driven programming in Mathematica is especially useful in constructing GUIs. It makes it possible to create functions that update the GUI when a new post is added, edited or retrieved, add them as callbacks and forget about them. Now every time you use the API, the GUI will automatically be updated to reflect the latest information. Below is a simple example of how to use callbacks:

nrOfUploadedFiles = 0;
countUploadedFiles[success[event_]] := If[
  event["method"] == "wp.uploadFile",
  nrOfUploadedFiles++;
  updateGUI[nrOfUploadedFiles];
  ]

AddCallback[countUploadedFiles]