2013-07-18 52 views
6

私はWordpressへの投稿にXMLRPCを使用しています。サムネイルを投稿する際に問題が発生しました。ワードプレスのコードをデバッグした後、私の問題は画像が投稿に添付されていないことが原因です。 私はwordpressにパッチを当てることなく、PHPを使用せずに、これを行う必要があります。iwth XMLRPCだけです。Wordpress XMLRPCに投稿する画像を添付する

画像をアップロードして画像のIDを取得できます。 私に混乱を招くその他の点は、画像がアップロードされるのを待っているので、まだ投稿していない投稿に画像を添付する方法です。私は画像をアップロードしてから投稿し、次に画像IDと投稿IDを使用して画像メタデータの更新を行うはずですか?

編集:問題になるワードプレスのコードは、このチェック

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

と私の仮定で、それは私がそのコードを修正する場合は、すべての罰金ですが、私はできない、画像が取り付けられていないので、それが失敗したということです

+0

はhttp://wp.tutsplus.com/tutorials/creative-coding/uploading-pictures-via-xml-rpc-and-php-to-wordpress/を参照してください。 – user1929959

+0

何らかの方法でPHPコードを操作することはできません。 –

答えて

7

はい、Wordpressのバージョンが3.5以上であれば、ファイル/画像のアップロードにコードを使用するとpost_idを設定できます。

  1. この

    によって返さpost_idのを記録し、falseに公開も設定newPost機能を使用して特色 画像なしでコンテンツを投稿すると:私は機能のイメージで新しい投稿のために使用さ の流れはこのようなものです
  2. 画像をアップロードして、ちょうど が投稿ポストのIDにpost_idの設定、編集にポストをやったときにimage_idは

  3. を記録し、に等しいwp_post_thumbnailを設定あなただけのアップロードも(必要な場合)、trueにパブリッシュ設定image_idは

重要

: MIMEタイプが重要であり、それは「画像/ JPG」または「画像/ PNG形式」でなければならないのドキュメントを参照してください、もし「jpg」添付が失敗するようなMIMEタイプは悪いです。

ヒント: デバッグでは、wordpressからの一般的なエラーが発生し、ワードプレスのコードをチェックしたり、編集したりすることができない場合は、デバッグ/トレースの呼び出しを追加してください。 。

これはカテゴリ、画像、タグを含む投稿の例です。それは、クラス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

 $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); 
+0

編集で2つの手順でこれを行う必要があるのはなぜですか?最初に画像をアップロードしてIDを取得し、IDをnewPost()に追加することができますか? –

+0

@RickStrahlその時点で私はすべてのステップが重要だったと信じていましたが、おそらく今は違うかもしれませんが、そのイメージをアップロードするにはpost_idを持ってポストに画像を添付し、アップロードされた画像、私の質問と答えは、XMLRPCを直接使用していた可能性がありますWPライブラリは、おそらくより少ないステップでそれを行うことができます – simion314

0

これはwp.newPostwp.editPostを使用して、私のバージョンであるが必要で、カスタムポストの使用を許可すること、ワードプレス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 >'); 
     } 
    } 
} 
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); 
関連する問題