Update your WordPress site from scripts

An example Python script, which can be re-used with other website engines.

WordPress logoThe Free Electrons website is proudly powered by WordPress. It is a mix of static pages and blog posts, and we are very satisfied of the way we can manage and post content.

Being the webmaster, I had an issue though. Several of our pages share the same bits of content, in particular the descriptions of our public training sessions. To avoid discrepancies between pages, I ended up writing scripts to generate the contents of these pages from common parts. However, updating those pages on the website was done with manual copying and pasting, which was time consuming and error prone.

Fortunately, after some on-line research and practical experiments, I found a simple way of automating the process of login to our WordPress site, open a page for editing, and submitting a new version of the contents.

Since this wasn’t really straightforward, I’m happy to share the update-wordpress-page Python script I came up with, hoping that it be useful to you too.

This script can only be used to modify an existing page. It just changes the contents, and doesn’t touch other attributes. Of course, you could also extend it to create new pages and posts, but this would represent much more work, having lots of input fields to fill.

Another approach would have been to open a direct database connection to the server running WordPress, and then to perform your updates directly with SQL commands. However, this requires a knowledge about WordPress databases (making the script much less generic), and the open database port also makes your website less secure.

To use my script, you will first have to find the edit URL for your page, which reveals the WordPress post id.

I suggest you to create a special WordPress user with Editor privileges. The page history will then show which changes were automated, and which were manual.

The last thing you will have to do is create a $HOME/.update-wordpress-page configuration file as follows:

[free-electrons.com/fr]
user=bot
password=ERrdrsdGp8

[free-electrons.com]
user=bot
password=Hgdeedxx55

You can easily tune this script to support other web content engines. You first need to identify the login page (WordPress uses cookies to authenticate a session, instead of simple http authentication). Then, you will find the names of the input forms by reading the login page HTML code.

The second step is to open the page editing URL, and find out the name of the input form used for the page contents.

We don’t offer official support for our script, but I hope that this working code example will help you to make your own scripts, and to get you started faster. Python’s urllib2 and ClientForm really make this easy to do. What I especially like with ClientForm is its ability to modify the value of a given form, without having to read and fill any other input forms in the page, to keep their default content.

About Michael Opdenacker

Michael Opdenacker is the founder of Free Electrons. He is best known for all the free embedded Linux and kernel training materials that he created together with Thomas Petazzoni. He is always looking for ways to increase performance, reduce size and boot time, and to maximize Linux' world domination. More details...
This entry was posted in Technical. Bookmark the permalink.

4 Responses to Update your WordPress site from scripts

  1. Brock Beatty says:

    Hi Michael,

    I’m working with your script and I’m unable to import ClientForm. Documentation that I’ve read on Python.org suggests that this has been incorporated into ‘mechanize’, and apparently supported in the 2.6.1 version of python that ships with Snow Leopard.

    I’m a Python noob but have experience with KSH, and UNIX in general. Any thoughts on what I’m doing wrong?

    ps. I’ve tried to run as script and compile and neither are working for me.

    ==> python -c ‘import mechanize’
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named mechanize

    ==> python -c ‘import ClientForm’
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named ClientForm

    thanks for sharing this script, I suspect once I get it working it will save me a lot of time. I’ll be sure to share how I’ve tweaked it for my purposes once this issues is resolved.

    Brock

    • You need to install the Python ClientForm module.

      I had to install it manually on Ubuntu 12.04:

      wget http://pypi.python.org/packages/source/C/ClientForm/ClientForm-0.2.10.tar.gz
      tar zxf ClientForm-0.2.10.tar.gz
      cd ClientForm-0.2.10
      sudo apt-get install python-setuptools
      python setup.py build
      sudo python setup.py install
      
  2. Also tested successfully on WordPress 3.1, 3.2 and 3.3, with Python 2.7.

Comments are closed.