In this post I'm gonna show how you can create pages on a WordPress blog from a Python script.

For this to work, you first have to install the plugin JSON REST API on your WordPress blog. We will also use the Python library Requests to simplify making HTTP requests. The quickstart for Requests contains a lot of useful information to get started.

What we want to do is to make a HTTP POST request to the REST API.

With curl installed (sudo apt-get install curl) we can make a HTTP POST against the API in the following way:

curl -v -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"title":"Title","content":"Content","type":"page"}' http://localhost/wp-json.php/posts -u admin:password

This where I found the curl command above.

Using curl with the verbose argument (-v) is a good way to perform HTTP debugging, for example to see how a REST API behaves.

The last argument in the curl command above is -u admin:password. This basically adds a header called Authorization to the request, encodes the string admin:password with Base64, and finally adds Basic along with the encoded string to the header field. The result is the header Authorization: Basic YWRtaW46cGFzc3dvcmQ=. This method of authentication is called Basic access authentication, and must in this case be added to be able to create a new WordPress page/post.

WordPress JSON REST API's complete schema can be read on their Github page, for additional properties when creating a post etc.

Doing this from Python can be done in the following way:

from base64 import b64encode
import json
import requests


def create_wordpress_page(title, content, publish):
    url = 'http://localhost/wp-json.php/posts'
    auth = b64encode('{}:{}'.format('admin', 'password'))
    payload = {
        'title': title,
        'type': 'page',
        'content_raw': content,
        'status': 'publish' if publish else 'draft',
    }
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Basic {}'.format(auth),
    }
    r = requests.post(url, data=json.dumps(payload), headers=headers)
    response = json.loads(r.content)
    return response['link']


create_wordpress_page('Title', 'Content', True)

Here we have used the two functions dumps and loads from the JSON module. dumps is used to encode Python data types to JSON, while loads is used to decode JSON.

comments powered by Disqus