Attach image to post in Wordpress XMLRPC

后端 未结 3 1065
时光说笑
时光说笑 2021-01-18 01:58

I am using XMLRPC to do posts to Wordpress. I am having issues posting thumbnails, after debugging wordpress code I see that my issue is caused by the fact that the image is

3条回答
  •  无人及你
    2021-01-18 02:27

    This is my version, using wp.newPost and wp.editPost, added on WordPress 3.4, that allow the use of custom post types.

    require_once("IXR_Library.php.inc");
    $title = 'My title';
    $body = 'My body';
    $category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
    $keywords="keyword1, keyword2, keyword3";
    $customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format
    
    $title = htmlentities($title,ENT_NOQUOTES,@$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,@$encoding);
    
    $content = array(
        'post_title'=>$title,
        'post_content'=>$body,
        'post_type'=>'some_custom_post_type',
        'post_status' => 'draft', // http://codex.wordpress.org/Post_Status
        'mt_allow_comments'=>0, // 1 to allow comments
        'mt_allow_pings'=>0, // 1 to allow trackbacks
        'mt_keywords'=>$keywords,
        'categories'=>array($category),
        'custom_fields' => array($customfields)
    );
    
    // Create the client object
    $client = new IXR_Client('http://example.com/xmlrpc.php');
    $username = "wp_username";
    $password = "wp_username_password";
    
    $params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'
    
    if (!$client->query('wp.newPost', $params)) {
        die('
    Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'
    '); } else { $post_id = $client->getResponse(); echo 'Inserted with id'.$post_id; $picture = '/full/path/to/pic.jpg'; $content = array( 'name' => basename($picture), 'type' => mime_content_type($picture), 'bits' => new IXR_Base64(file_get_contents($picture)), true ); if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) { die('
    Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'
    '); } else { $media = $client->getResponse(); $content = array( 'post_status' => 'publish', 'post_thumbnail' => $media['id'] ); if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) { die('
    Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'
    '); } } }

提交回复
热议问题