Adding Image Alternate Text into WordPress Image Attachment using python-wordpress-xmlrpc Library

Problem

I'm using python-wordpress-xmlrpc to create a post remotely into WordPress, all doing fine except when I'm uploading an attachment image, I can't set image alternate text (alt="") to the corresponding image.

Solution

  1. Create a custom WordPress plugin to extend the built-in WordPress XMLRPC method.
  2. Create a class in your python script to access that custom method.

Example

Here is the custom plugin I mentioned above [link]:

<?php
/**
 * Plugin Name: xmlrpc-extend
 * Description: xmlrpc-extend
 * Author: edgar galliamov
 * Author URI:
 * Version: 0.1
 * Plugin URI: https://github.com/g-gabber
 */

/***************************************************************
 * SECURITY : Exit if accessed directly
***************************************************************/
if ( !defined( 'ABSPATH' ) ) {
    die( 'Direct acces not allowed!' );
}



function wp_editMedia( $args ) {
    global $wp_xmlrpc_server;
    $wp_xmlrpc_server->escape( $args );

    $blog_id  = $args[0];
    $username = $args[1];
    $password = $args[2];
    $post_id  = (int) $args[3];
    $content_struct = $args[4];

    if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) )
        return $wp_xmlrpc_server->error;


    if ( FALSE === get_post_status( $post_id ) ) {        
        // The post does not exist      
        return 0;
    } else {          
        // The post exists  

        $my_post = array( 'ID' => $post_id );

        if (array_key_exists("title", $content_struct)) {
            $my_post['post_title'] = $content_struct['title'];  
        }
        if (array_key_exists("description", $content_struct)) {
            $my_post['post_content'] = $content_struct['description'];  
        }
        if (array_key_exists("caption", $content_struct)) {
            $my_post['post_excerpt'] = $content_struct['caption'];  
        }

        if (count($my_post) > 1){
            // Update the post into the database
            wp_update_post( $my_post );
        }       

        if (array_key_exists("image_alt", $content_struct)) {
            if ( ! add_post_meta($post_id, '_wp_attachment_image_alt', $content_struct['image_alt'], true)){
                update_post_meta( $post_id, '_wp_attachment_image_alt', $content_struct['image_alt']);
            }
        }

        return $my_post;
    }


}

function new_xmlrpc_methods( $methods ) {
    $methods['wp.editMedia'] = 'wp_editMedia';
    return $methods;   
}
add_filter( 'xmlrpc_methods', 'new_xmlrpc_methods');
?>

And here is my custom python class to access that method.

from wordpress_xmlrpc import AuthenticatedMethod


class EditImageAlt(AuthenticatedMethod):
        method_name = 'wp.editMedia'
        method_args = ('imgID', 'imgalt')

print("updating image alt", img["id"])
wpObject.call(EditImageAlt(img["id"], {"image_alt": image_alt}))
print("image alt added!")


Reference

Comments

Post a Comment

Popular posts from this blog

Keutamaan Tidur yang Sedikit Menurut para Ulama

Kisah Nabi Uzair a.s.