An example Python script, which can be re-used with other website engines.
The 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
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.