Connecting Python to WordPress, Part 2

Earlier, I discussed how to automate posting to WordPress with python. However, I wanted to add a Description and “Alternative Text” to my images (charts). This turned out to be much more difficult than I anticipated.


To update manually, you would click on an image in the WordPress media library and edit the fields.

Straightforward stuff. That is, until you have a lot of images to which you want to add alternative text! The below steps allows you to update these fields using the Python WordPress xml-rpc package.


Step 1.

If you aren’t already doing so, I highly recommend you start using a child theme, before editing functions.php at your website.

To change the alt-text of an image programmatically, you have to update your functions.php file first. The reason: python will be adding a custom field to the WordPressPost object for the image, “_wp_attachment_image_alt”.  This field is a protected field, and the PHP code snippet below unprotects it:


add_action( 'init', function() {
     register_meta( 'post', '_wp_attachment_image_alt', 'string', '__return_true' );
});

 

Step 2.

Next you will see updated Python code from my post in Part 1.  The major change is that Images is now a Python dict.  Each key in Images will be used to populate the alt-text & description fields. The value of the Images key-value pair is the locally saved filename. FeatImg is a string specifying the key in Images that will be used as the Featured Image. I have highlighted the important new lines.

 


from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts 
import os


def post_to_wordpress(Images,FeatImg,Title,Slug,Body,Tags,Ctgry,Excerpt,Publish=False):
 # Connect to wordpress 
 wp = Client('http://yourwebsite.com', 'user', 'password') 

 # Upload figures to website
 for i,key in enumerate(Images):
    FileName = Images[key]
    imagename = os.path.split(FileName)[1]
    imagetype = imagename.split('.')[1]
    data = {'name': imagename,'type': 'image/'+imagetype,'overwrite':True}
    with open(FileName, 'rb') as img:
       data['bits'] = xmlrpc_client.Binary(img.read())
    response = wp.call(media.UploadFile(data))
    wppimage = wp.call(posts.GetPost(response['id']))
    wppimage.custom_fields = [{'key':'_wp_attachment_image_alt','value':key}]
    wppimage.content = key
    success = wp.call(posts.EditPost(response['id'],wppimage))
    if not success: print 'Editing alt text for image [{}] failed'.format(key)
    if FeatImg is not None: 
       if key==FeatImg: attachment_id = response['id']

 # Create the post
 post = WordPressPost()
 post.title = Title
 post.slug = Slug
 post.content = Body
 post.terms_names = {'post_tag': Tags,'category': Ctgry }
 post.thumbnail = attachment_id
 post.post_format = 'Standard' # Image
 post.excerpt = Excerpt
 post.comment_status = 'open'

 # Publish
 if Publish: post.post_status = 'publish'
 post.id = wp.call(posts.NewPost(post))

 
 

Breaking Away

This took quite some time to work out. The documentation and StackOverflow inquiries on this topic are sparse. However, now that I have figured it out, I will be able to improve my SEO (slightly?) and speed up my posting process. If you benefit from this, enjoy!

You may also like...