xml rpc - Attach image to post in Wordpress XMLRPC -


i using xmlrpc posts wordpress. having issues posting thumbnails, after debugging wordpress code see issue caused fact image not attached post. must without patching wordpress or using php, iwth xmlrpc.

i can upload image , id of image. other point confuses me how attach image post did not posted yet because wait image upload? supposed upload image post ,then using image id , post id update on image metadata?

edit: code in wordpress problematic check

if ( $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) 

and assumption fails because image unattached, if fix code fine can't patch wp of application users(so not solution)

yes possible it, if wordpress version 3.5 or greater,when using code uploading file/image can set post_id. flow used new posts featured images this:

  1. use newpost function , post content without featured image , set publish false, record post_id returned this

  2. upload image , set post_id id of post posted, record image_id

  3. when done edit post , set wp_post_thumbnail equal image_id uploaded , set publish true(if needed)

important: mime type important, must "image/jpg" or "image/png" please see documentation, if mime type worng "jpg" attaching fail.

tip: debugging, if generic error wordpress , can't figure out why can check wordpress code , edit it, adding debugging/tracing calls , can figure out cause.

this example of post category, image , tags. requires class-ixr.php
https://github.com/wordpress/wordpress/blob/master/wp-includes/class-ixr.php
, mime_content_type function
https://github.com/caiofior/storebaby/blob/master/magmi/plugins/extra/general/socialnotify/wp/mimetype.php

        $client = new ixr_client($url);         $content = array(             'post_status' => 'draft',             'post_type' => 'post',             'post_title' => 'title',             'post_content' => 'message',              // categories ids             'terms' => array('category' => array(3))         );         $params = array(0, $username, $password, $content);         $client->query('wp.newpost', $params);         $post_id = $client->getresponse();          $content = array(             'name' => basename('/var/www/sb/img.jpeg'),             'type' => mime_content_type('/var/www/sb/img.jpeg'),             'bits' => new ixr_base64(file_get_contents('/var/www/sb/img.jpeg')),             true         );         $client->query('metaweblog.newmediaobject', 1, $username, $password, $content);         $media = $client->getresponse();         $content = array(             'post_status' => 'publish',             // tags             'mt_keywords' => 'tag1, tag2, tag3',             'wp_post_thumbnail' => $media['id']         );         $client->query('metaweblog.editpost', $post_id, $username, $password, $content, true); 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -