附加图像以在 Wordpress XMLRPC 中发布 [英] Attach image to post in Wordpress XMLRPC

查看:32
本文介绍了附加图像以在 Wordpress XMLRPC 中发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 not attached to the post. I must do this without patching wordpress or using PHP, only iwth XMLRPC.

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

I can upload an image and get the ID of the image. Other point that confuses me is how do you attach an image to a post that you did not posted yet because you wait for the image to upload? I am supposed to upload image then post ,then using the image id and the post id do an update on the image metadata?

wordpress 中有问题的代码是这个检查

the code in wordpress that is problematic is this check

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

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

and my assumption it is that it fails because the image is Unattached, if i fix that code all is fine but I can't patch the WP of my application users(so this is not a solution)

推荐答案

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

Yes it is possible to do it, if Wordpress version is 3.5 or greater,when using the code for uploading file/image you can set the post_id. The flow I used for new posts with featured images is like this:

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

  1. use the newPost function and post the content without the featured image and also set publish to false, record the post_id returned by this

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

upload the image and set the post_id to the id of the post just posted, record the image_id

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

when done edit the post and set the wp_post_thumbnail equal to the image_id you just uploaded and also set publish to true(if needed)

重要:mime 类型很重要,它必须是image/jpg"或image/png"请看文档,如果 mime 类型被磨损g 像jpg"附加将失败.

Important: The mime type is important, it must be "image/jpg" or "image/png" please see documentation, if mime type is worng like "jpg" attaching will fail.

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

Tip: For debugging, if you get a generic error from wordpress and you can't figure out why you can check the wordpress code and even edit it, adding debugging/tracing calls and hopefully you can figure out the cause.

这是一个包含类别、图片和标签的帖子示例.它需要类-IXR.php
https://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

This is an example of a post with category, image and tags. It requires class-IXR.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php
and 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);

这篇关于附加图像以在 Wordpress XMLRPC 中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆