scottgonzalez/node-wordpress

How to set image's alternate text?

Closed this issue · 10 comments

Currently I'm trying to set _wp_attachment_image_alt using editPost, but it seems that this is is unique. Is there any other way to set it?

client.editPost(attachmentId, {
    content: "description",
    excerpt: "caption",
    status: "inherit",
    customFields: [{"id": null, "key": "_wp_attachment_image_alt", "value": "alternate text"}]
}, function (error) {
    console.log(error);
});

Not sure if this the right please to ask such a question.

I haven't personally managed images like this, so I'm not sure what you need to do. Can you set the alt text via the admin, call getPost() and post the results? That should help figure out the editPost() call.

When you are uploading images via admin panel you can set image's alt text.
image
I case of your library, using a

client.uploadFile

you can't set alt text. So my idea was using attachment id to set alt text using

client.editPost

but it does not work.

I'll ask again:

Can you set the alt text via the admin, call getPost() and post the results?

I'm guessing that getPost() won't return the image because images aren't posts, they're media items. Which is why I want to see what you're getting back.

I see, when you upload images wordpress handle them as post but post_type is set to attachment.
There is a table wp_postmeta which contains custom fields for each post no matter which type is it.
Alt text is stored as _wp_attachment_image_alt custom field.

{ id: '2073',
     title: 'title 3',
     date: Sun Jun 12 2016 12:02:05 GMT+0200 (Mitteleuropäische Sommerzeit),
     modified: Mon Jun 13 2016 11:45:32 GMT+0200 (Mitteleuropäische Sommerzeit),
     status: 'inherit',
     type: 'attachment',
     name: 'test-image-jpg',
     author: '1',
     password: '',
     excerpt: 'caption 6',
     content: 'description 4',
     parent: '2066',
     link: 'http://localhost/test-image-jpg/',
     menuOrder: 0,
     commentStatus: 'open',
     pingStatus: 'closed',
     sticky: false,
     thumbnail: [],
     format: 'standard',
     terms: [],
     customFields: [] } 

Are you sure this isn't content or excerpt? If you have alt text set on that image and it's not coming back from getPost(), then I would guess that you can't manage it via the XML-RPC API. At least not without creating your own custom XML-RPC methods.

I already did it via custom XML-RPC method

Oh, well, then you can call that method using client.call() or client.authenticatedCall(). Sorry that I couldn't provide much help with this. It does seem strange that WordPress doesn't expose this.

I thought there is a regular way to set it.
Actually the problem is here. If the fourth parameter of add_post_meta would be set to true, the it would work. Anyway thank you very much for your support!

I will provide my solution here. Just put in into wp-plugins

<?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');
?>