How to Upload using WordPress MetaWeblog.newMediaObject and IXR_Library

How to Upload using WordPress MetaWeblog.newMediaObject and IXR_Library

Continuing my XMLRPC article, I will show how to use wordpress image upload metaWeblog.newMediaObject and IXR_Library.

I still to use IXR_Library class to upload media remotely on wordpress, where previously I apply to my article on Remote wordpress post with XML-RPC metaWeblog.newPost and IXR_Library Class

The advantage of using IXR_Library is IXR_Base64 classes that facilitate the use of base64 code in bits parameter in metaWeblog.newMediaObject.

The parameters used in metaWeblog.newMediaObject

  • blogid (int).
  • username (string ).
  • password (string)
  • struct data
    • name => Filename (string).
    • type => File MIME type (string).
    • bits => base64-encoded binary data (string).
    • overwrite => Bool ( Optional – Overwrite an existing attachment of the same name).

 Okay, lets begin the tutorial Upload using WordPress MetaWeblog.newMediaObject and IXR_Library 

1. First , you need to download IXR_Library.php here.

2. Create new php file as upload_image.php, and put IXR_Library.php in the same folder with it. Don’t forget to put your image in your directory (In my example folder/image.jpg)

3. Copy this script below on upload_image.php

Explanation

We need to convert image file into string using fread php, ( see fread documentation here)

 Use new IXR_Base64 to encode $data

$XmlRpc_client->getResponse() will send back the return value.

Get image id and combine it with metaWeblog.newPost  to set featured image. To set featured image in your post, create custom_fields with key ‘_thumbnail_id’ and value ‘image_id’. see the example below

[php highlight=”1,14″]
$customfields=array(‘key’=>’_thumbnail_id’, ‘value’=>’53’); // set featured image here

$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

$content = array(
‘title’=>$title,
‘description’=>$body,
‘mt_allow_comments’=>0, // 1 to allow comments
‘mt_allow_pings’=>0, // 1 to allow trackbacks
‘post_type’=>’post’,
‘mt_keywords’=>$keywords,
‘categories’=>array($category),
‘custom_fields’ => array($customfields) , // how to set custom_fields
‘date_created_gmt’ => $date
);

Thus tutorial How to upload using WordPress MetaWeblog.newMediaObject and IXR_Library, if you need any question please leave the comment. Read another wordpress xmlrpc tutorial here.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

2 Comments

 Add your comment
  1. ERROR Here. Parse error: syntax error, unexpected ‘=’, expecting ‘)’
    The code is OK, but generate error…. help maybe IXR_Base64

    $content = array
    (
    ‘name’ = ‘image_name.jpg’,
    ‘type’ = ‘image/jpeg’,
    ‘bits’ = new IXR_Base64 ($data)
    );

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.