Create a WordPress Post from Node.js

Watch out! This tutorial is over 7 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:

npm install wordpress

and it’s basic usage can be summarised with:

var wordpress = require( "wordpress" );
var client = wordpress.createClient({
    url: "my-site.com",
    username: "admin",
    password: "secret"
});
 
client.getPosts(function( error, posts ) {
    console.log( "Found " + posts.length + " posts!" );
});

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.

var wordpress = require( "wordpress" );
var client = wordpress.createClient({
    url: "10.108.155.15",
    username: "student",
    password: "1Password"
});
 
client.newPost({
        title: "Post from Node.js",
        content: "This post was sent remotely from Node.js.....",
        status: "publish",
        termNames: {
                "category": ["Javascript", "Node"],
                "post_tag": ["api", "js", "remote"]
        }
}, function( error, data ) {
        console.log( "Post sent! The server replied with the following:\n" );
        console.log( arguments );
        console.log("\n");
});

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

nodejs wordpress.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.

var fs = require( "fs" );
var wordpress = require( "wordpress" );
var client = wordpress.createClient({
    url: "10.108.155.15",
    username: "student",
    password: "1Password"
});

var filename = "aurora-borealis.jpg";
var file = fs.readFileSync( filename );
client.uploadFile({
	name: filename,
	type: "image/jpg",
	bits: file
}, function( error, data ) {
	console.log( arguments );
});

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…