How to create new post with photo attached in WordPress using XMLRPC?

前端 未结 6 2241
忘了有多久
忘了有多久 2020-12-15 13:44

Anyone knows how to create new post with photo attached in WordPress using XMLRPC?

I am able to create new post and upload new picture separately,

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 13:58

    When you post, WordPress will scan at the post for IMG tags. If WP finds the image, it's loaded in it's media library. If there's an image in the body, it will automatically attached it to the post.

    Basically you have to:

    • post the media (image) first
    • Grab its URL
    • include the URL of the image with a IMG tag in the body of your post.
    • then create the post

    Here is some sample code. It needs error handling, and some more documentation.

    $admin ="***";
    $userid ="****";
    $xmlrpc = 'http://localhost/web/blog/xmlrpc.php';
    include '../blog/wp-includes/class-IXR.php';
    $client = new IXR_Client($xmlrpc);
    
    $author         =   "test";    
    $title          =   "Test Posting";
    $categories     =   "chess,coolbeans";
    $body           =   "This is only a test disregard 
    "; $tempImagesfolder = "tempImages"; $img = "1338494719chessBoard.jpg"; $attachImage = uploadImage($tempImagesfolder,$img); $body .= ""; createPost($title,$body,$categories,$author); /* */ function createPost($title,$body,$categories,$author){ global $username, $password,$client; $authorID = findAuthor($author); //lookup id of author /*$categories is a list seperated by ,*/ $cats = preg_split('/,/', $categories, -1, PREG_SPLIT_NO_EMPTY); foreach ($cats as $key => $data){ createCategory($data,"",""); } //$time = time(); //$time += 86400; $data = array( 'title' => $title, 'description' => $body, 'dateCreated' => (new IXR_Date(time())), //'dateCreated' => (new IXR_Date($time)), //publish in the future 'mt_allow_comments' => 0, // 1 to allow comments 'mt_allow_pings' => 0,// 1 to allow trackbacks 'categories' => $cats, 'wp_author_id' => $authorID //id of the author if set ); $published = 0; // 0 - draft, 1 - published $res = $client->query('metaWeblog.newPost', '', $username, $password, $data, $published); } /* */ function uploadImage($tempImagesfolder,$img){ global $username, $password,$client; $filename = $tempImagesfolder ."/" . $img; $fs = filesize($filename); $file = fopen($filename, 'rb'); $filedata = fread($file, $fs); fclose($file); $data = array( 'name' => $img, 'type' => 'image/jpg', 'bits' => new IXR_Base64($filedata), false //overwrite ); $res = $client->query('wp.uploadFile',1,$username, $password,$data); $returnInfo = $client->getResponse(); return $returnInfo['url']; //return the url of the posted Image } /* */ function findAuthor($author){ global $username, $password,$client; $client->query('wp.getAuthors ', 0, $username, $password); $authors = $client->getResponse(); foreach ($authors as $key => $data){ // echo $authors[$key]['user_login'] . $authors[$key]['user_id'] ."
    "; if($authors[$key]['user_login'] == $author){ return $authors[$key]['user_id']; } } return "not found"; } /* */ function createCategory($catName,$catSlug,$catDescription){ global $username, $password,$client; $res = $client->query('wp.newCategory', '', $username, $password, array( 'name' => $catName, 'slug' => $catSlug, 'parent_id' => 0, 'description' => $catDescription ) ); }

提交回复
热议问题