How to post content to WordPress using Python and Rest API



To get familiar with WordPress REST API, I fired up Python started playing with the requests module, the elegant and simple HTTP library for Python, built for human beings. Python and the WordPress REST API Handbook gave me enough information to get started.

The default cookies authentication mechanism would not work with Python and you need to install a plugin for this sort of application. See this tutorial on how to use the WordPress OAuth 1.0a plugin.

For a quick demo, the Application Passwords plugin for WordPress is the easiest choice. The installation is straightforward, and the instructions to generate a password for the client application are quite simple to follow. I needed to modify the .htaccess file due to the way my DreamPress site is configured. Notice: the password generated by the plugin contains white spaces, and they are part of the password. Once the WordPress authentication is taken care of, it’s all much easier.

On to Python, get json and requests modules ready, set the base URL of your WordPress site, your username and the application password generated by the plugin (including the white spaces):

import requests
import json

user = 'username'
pythonapp = 'G4kN hBNh r35J luXk aXyd n6Lm'
url = ''

The Application Password plugin requires a token made of username and password encoded in base64, so let python create it and add that to the http headers:

token = base64.standard_b64encode(user + ':' + pythonapp)
headers = {'Authorization': 'Basic ' + token}

Next, prepare some demo content for a new post. WordPress API expect a JSON object:

post = {'date': '2017-06-19T20:00:35',
        'title': 'First REST API post',
        'slug': 'rest-api-1',
        'status': 'publish',
        'content': 'this is the content post',
        'author': '1',
        'excerpt': 'Exceptional post!',
        'format': 'standard'

That’s all you need to create a post:

r = + '/posts', headers=headers, json=post)
print('Your post is published on ' + json.loads(r.content)['link'])

You’ll see printed on screen the URL of the new post. That was fun and quick :slight_smile: Now let’s publish an image: first you have to add it to the Media library (as you would when you use WordPress admin panel). To publish images you’ll need to use the media API endpoint. For the example, use the file demo.jpg in the current directory

media = {'file': open('demo.jpg',rb),
'caption': 'My great demo picture'}

And let Python requests do the heavy lifting:

image = + '/media', headers=headers, files=media)
print('Your image is published on ' + json.loads(image.content)['link'])

That should give you the URL for the image you just uploaded. Now to embed that image in a post, we can edit its content. To update a post we need to find its ID and push to the API endpoint the new value using JSON.

imgsrc = json.loads(up.content)['source_url']
postid = json.loads(r.content)['id']
updatedpost = {'content': 'Changed things.<img src=' 
        + imgsrc
        + '>'}

Update the post with the new content:

update = + '/posts/' + postid, headers=headers, json=updatedpost)
print('The updated post is published on ' + json.loads(updatedpost.content)['link'])

And that’s all: you created a new post, added an image to WordPress media library and modified a post using only Python and the REST API.