Attach image to post in Wordpress XMLRPC

后端 未结 3 1073
时光说笑
时光说笑 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:34

    My version if you want to use only wp.newPost and 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.')
    Post ID is '.$postID.'
    '; } //////////// 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);

提交回复
热议问题