将图像附加到 Wordpress XMLRPC 中的帖子

Posted

技术标签:

【中文标题】将图像附加到 Wordpress XMLRPC 中的帖子【英文标题】:Attach image to post in Wordpress XMLRPC 【发布时间】:2013-07-17 08:39:57 【问题描述】:

我正在使用 XMLRPC 向 Wordpress 发帖。我在发布缩略图时遇到问题,在调试 wordpress 代码后,我发现我的问题是由于图像未附加到帖子的事实引起的。 我必须在不修补 wordpress 或使用 php 的情况下执行此操作,仅使用 XMLRPC。

我可以上传图片并获取图片的 ID。 让我感到困惑的另一点是,您如何将图像附加到您尚未发布的帖子中,因为您等待图像上传?我应该上传图像然后发布,然后使用图像 ID 和帖子 ID 对图像元数据进行更新?

编辑:wordpress 中有问题的代码就是这个检查

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

我的假设是它失败是因为图像未附加,如果我修复该代码一切都很好,但我无法修补我的应用程序用户的 WP(所以这不是解决方案)

【问题讨论】:

见wp.tutsplus.com/tutorials/creative-coding/…。 不以某种方式操作 PHP 代码是不可能的。 【参考方案1】:

如果你只想使用 wp.newPost 和 wp.editPost,我的版本

include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );

    $usr = 'username_on_the_server_side';
    $pwd = 'password_on_the_server_side';
    $xmlrpc = 'server side xmlrpc.php url';
    $client = new IXR_Client($xmlrpc);

    ////////////  IMAGE UPLOAD AND ATTACHMENT POST CREATION  ///////////
       $img_attach = 'link to the image';
       $img_attach_content = array(
                'name' => basename($img_attach),
                'type' => mime_content_type($img_attach),
                'bits' => new IXR_Base64(file_get_contents($img_attach)),
                        );
        $status = $client->query( 'wp.uploadFile','1',  $usr, $pwd, $img_attach_content );
        $image_returnInfo = $client ->getResponse();

   ////////////  POST CREATION  ///////////

        $custom_fields = array( 
                          array( 'key' => 'blabla1', 'value' => 'blabla1_value' ),
                          array( 'key' => 'blabla12', 'value' => 'blabla1_value2')
                          ); 
        $post_content = array(
            'post_type' => 'post',
            'post_status' => 'draft', //for now
            'post_title' => 'XMLRPC Test',
            'post_author' => 3,
            'post_name' => 'XMLRPC Test',
            'post_content' => 'XMLRPC Test Content',
            'custom_fields' => $custom_fields
        );

    $res = $client -> query('wp.newPost',1, $usr, $pwd, $post_content);
    $postID =  $client->getResponse();
    if(!$res)
        echo 'Something went wrong....';
    else 
            echo 'The Project Created Successfully('.$res.')<br>Post ID is '.$postID.'<br>';
    

   ////////////  Image Post Attachment Edit  ///////////
      $img_attach_content2 = array(
                'post_type'  => 'attachment',   
                'post_status' => 'inherit', 
                'post_title' => $postID, 
                'post_name' => $postID, 
                'post_parent'  => $postID,
                'guid'    => $image_returnInfo['url'],
                'post_content'   => '',
                'post_mime_type' => 'image/jpg'
                 );

     $res2 = $client -> query('wp.editPost', 0, $usr, $pwd,      $image_returnInfo['id'], $img_attach_content2);

    $postIDimg =  $client->getResponse();    

    ////////////   POST EDIT  ///////////

      $post_content2 = array(
                 'post_status' => 'publish', //publish
                'wp_post_thumbnail' => $image_returnInfo['id'],
                'custom_fields' =>    array( 'key' => '_thumbnail_id', 'value' =>  $image_returnInfo['id'] ) 
            );
            $media2= $client->query('wp.editPost',0, $usr, $pwd, $postID, $post_content2);

【讨论】:

【参考方案2】:

这是我的版本,使用wp.newPost 和wp.editPost,添加到WordPress 3.4,允许使用自定义帖子类型

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('<br/><strong>Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');

    
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('<br/><strong>Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
    
    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('<br/><strong>Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
        
    

【讨论】:

【参考方案3】:

是的,可以这样做,如果 Wordpress 版本是 3.5 或更高版本,当使用代码上传文件/图像时,您可以设置 post_id。 我用于带有特色图片的新帖子的流程是这样的:

    使用 newPost 功能并发布没有特色的内容 image 并同时设置 publish 为 false,记录返回的 post_id 这个

    上传图片并将post_id设置为刚刚发布的id 发布,记录image_id

    完成后编辑帖子并将 wp_post_thumbnail 设置为等于 您刚刚上传的 image_id 并将 publish 设置为 true(如果需要)

重要: mime 类型很重要,必须是 "image/jpg" 或 "image/png" 请参阅文档,如果使用 mime 类型如 "jpg" 附加将失败。

提示: 对于调试,如果您从 wordpress 收到一般错误,并且您不知道为什么可以检查 wordpress 代码甚至对其进行编辑,添加调试/跟踪调用,希望您能找出原因。

这是一个包含类别、图像和标签的帖子示例。它需要 class-IXR.phphttps://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php 和 mime_content_type 函数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);

【讨论】:

为什么需要在编辑中分两步完成?在我看来,您可以先上传图片,捕获 id,然后在 newPost() 中附加 Id? @RickStrahl 相信我当时所有这些步骤都很重要,也许现在不同了,但是要上传该图像,您必须具有 post_id 才能将图像附加到帖子,并设置特色图片你需要上传图片的id,我的问题和答案是直接使用XMLRPC,没有WP库可以用更少的步骤完成

以上是关于将图像附加到 Wordpress XMLRPC 中的帖子的主要内容,如果未能解决你的问题,请参考以下文章

wordpress-从附加到post的图像自动创建灰盒图像集

如何强制WordPress Gutenberg刷新特色图片?

WordPress XMLRPC 的问题

从 Wordpress 媒体库中获取单个特定图像

解决WordPress网站被利用xmlrpc.php文件攻击问题

sh wordpress xmlrpc测试