2013-07-18 33 views
6

Tôi đang sử dụng XMLRPC để thực hiện các bài đăng lên Wordpress. Tôi đang gặp vấn đề khi đăng hình thu nhỏ, sau khi gỡ lỗi mã wordpress tôi thấy rằng vấn đề của tôi là do thực tế là hình ảnh không được đính kèm vào bài đăng. Tôi phải làm điều này mà không cần vá wordpress hoặc sử dụng PHP, chỉ với XMLRPC.Đính kèm hình ảnh để đăng trong Wordpress XMLRPC

Tôi có thể tải lên hình ảnh và nhận ID của hình ảnh. Điểm khác gây nhầm lẫn cho tôi là cách bạn đính kèm hình ảnh vào bài đăng mà bạn chưa đăng vì bạn đợi hình ảnh tải lên? Tôi phải tải lên hình ảnh sau đó đăng bài, sau đó sử dụng id hình ảnh và id bài đăng cập nhật trên siêu dữ liệu hình ảnh?

Chỉnh sửa: mã trong wordpress mà là vấn đề là kiểm tra

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

này và giả thiết của tôi nó là nó thất bại bởi vì hình ảnh là Không được đính kèm, nếu tôi sửa chữa mã mà tất cả là tốt nhưng tôi không thể vá WP của người dùng ứng dụng của tôi (vì vậy đây không phải là giải pháp)

+0

Xem http://wp.tutsplus.com/tutorials/creative-coding/uploading-pictures-via-xml-rpc-and-php-to-wordpress/. – user1929959

+0

nó là không thể mà không cần thao tác mã PHP trong một số cách. –

Trả lời

7

Có thể thực hiện, nếu phiên bản Wordpress là 3.5 hoặc cao hơn, khi sử dụng mã để tải lên tệp/hình ảnh, bạn có thể đặt post_id. Dòng Tôi sử dụng cho bài viết mới với những hình ảnh đặc sắc là như thế này:

  1. sử dụng chức năng newPost và đăng các nội dung nếu không có sự đặc trưng hình ảnh và cũng thiết lập xuất bản false, ghi lại post_id trả về bởi

    này
  2. tải hình ảnh và thiết lập post_id đến id của bài chỉ đăng, ghi lại image_id

  3. khi thực hiện chỉnh sửa bài viết và thiết lập wp_post_thumbnail bằng image_id bạn chỉ cần tải lên và cũng có thể thiết lập xuất bản là true (nếu cần)

Chú ý: Các loại mime là rất quan trọng, nó phải là "image/jpg" hoặc "image/png" xin vui lòng xem tài liệu, nếu loại mime là worng như "jpg" gắn sẽ thất bại.

Mẹo: Để gỡ lỗi, nếu bạn gặp lỗi chung từ wordpress và bạn không thể tìm ra lý do tại sao bạn có thể kiểm tra mã wordpress và thậm chí chỉnh sửa nó, thêm gỡ lỗi/truy tìm cuộc gọi và hy vọng bạn có thể tìm ra nguyên nhân .

Đây là ví dụ về bài đăng có danh mục, hình ảnh và thẻ. Nó đòi hỏi đẳng cấp IXR.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php
và mime_content_type chức năng
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

Tại sao cần thực hiện điều này trong hai bước với Chỉnh sửa? Dường như với tôi bạn có thể tải lên hình ảnh đầu tiên, chụp id, sau đó đính kèm Id trong newPost()? –

+0

@RickStrahl tin tưởng tôi vào thời điểm đó tất cả các bước này đều quan trọng, có thể bây giờ là khác, nhưng để tải lên hình ảnh đó bạn phải có post_id để đính kèm hình ảnh vào bài đăng và đặt hình ảnh nổi bật bạn cần id của hình ảnh đã tải lên, câu hỏi và câu trả lời của tôi là sử dụng XMLRPC trực tiếp và không có thư viện WP nào có thể thực hiện trong ít bước hơn – simion314

0

Đây là phiên bản của tôi, sử dụng wp.newPostwp.editPost, gia tăng đối với WordPress 3.4, cho phép người sử dụng tùy chỉnh bài các loại.

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

Phiên bản của tôi nếu bạn chỉ muốn sử dụng wp.newPost và 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); 
Các vấn đề liên quan