Create a WordPress Post from Node.js

Watch out! This tutorial is over 3 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

WordPress uses an XML-RPC interface by default. XML-RPC is a remote procedure call (RPC) protocol which uses XML to encode its calls and HTTP as a transport mechanism. With WordPress XML-RPC support, you can post to your WordPress blog using many popular clients. You could even consider writing your own client application using Node.js. Let’s do a proof of concept.

node-wordpress is a nodejs JavaScript client for working with WordPress. You install it with:

and it’s basic usage can be summarised with:

For our proof of concept, you should fire up 2 servers (from existing templates) inside the XenServer Live Environment.  You will need a Node.js Server and a WordPress Server. Take note of the IP addresses of both (gotten with ifconfig at the console), and be sure to complete the WordPress install in your browser (or the following won’t work!).

Now, remoting to the the Node.js server via SSH, create  a new script and swap our example values for the IP address, username and password of your WordPress install. Yours will be most likely different than what we use.

Run the script in the usual manner for Node.js:

then go check your WordPress website. You should see a new post, that has been published to 2 new categories (“Javascript” and “Node“) and has tags of “api”, “js” and “remote”.

This means it would be entirely possible to write an entire client application for WordPress using Node.js. Why would you do this? Two reasons.

  • Speed. Node.js is crazy fast, even faster than PHP7 on NGINX in benchmark tests. A client written in Node.js would allow a busy website editor to work much, much faster.
  • More Speed. Node.js allows for asynchronous programming. Asynchronous I/O is a form of input/output processing that permits other processing to continue before the transmission has finished. In our scenario, Node.js does not need to wait on a database transaction to finish the way PHP might.

Need more convincing? Here is how you might upload a file to your WordPress server.

You can also delete and edit existing posts via this module, so building a fast, Node.js powered WordPress client is entirely possible! Keep it in mind for your next big project…