How to add excerpt, tags, categories and custom fields to wordpress posts with remote publishing by xmlrpc

WordPress offers a great xmlrpc support using metaweblog Api for remote publishing. Users find it highly useful for posting on the fly, especially if you are running many blogs. With this you do not need to login to your blog time and again for posting. If you have ever used remote publishing for posting you must have noticed that inspite of its usefulness it presents you with some limitations.

WordPress default xmlrpc has many limitations.

  1. Unable to add tags and categories for your post
  2. Include an excerpt with the post
  3. Add custom fields with key and value pair.

How to remove wordpress xmlrpc limitations?

You can modify xmlrpc.php file so that it can handle excerpts, tags, categories and custom fields. I will show you what modifications you need to make in your file and where.

Locate your xmlrpc.php file and open it in some code editor. Xmlrpc.php file is located in the root directory of your blog installation. What code editor you use depends on you. I am using notepad++. You may use the online code editor located in your file manager.

Now make following changes. Go to line 371 and add follow code.

add_meta($post_id);
}
}
}
/**
* Set taxonomies for post
*
* @param int $post_id Post ID.
* @param array $fields Taxonomy Fields
* Enter Values as array
* array ( 'tags' => '', 'taxonomy' => '' )
*/
function set_new_taxonomy_tag($post_id, $fields) {
$post_id = (int) $post_id;

foreach ( (array) $fields as $tax ) {
if ( isset($tax['id']) ) {
$tax['id'] = (int) $tax['id'];

if ( isset($tax['taxonomy']) ) {
wp_set_post_terms($tax['id'], $tax['tags'], $tax['taxonomy']);
}
}
elseif ($post_id != '') {

if ( isset($tax['taxonomy']) ) {
wp_set_post_terms($post_id, $tax['tags'], $tax['taxonomy']);
}
}
}
}

Now go to line 2241 and add this code.

if ( isset($content_struct['custom_fields']) ) {
$this->set_custom_fields($post_ID, $content_struct['custom_fields']);
}

//insert taxonomies
if ( isset($content_struct['mt_taxonomy']) ) {
$this->set_new_taxonomy_tag($post_ID, $content_struct['mt_taxonomy']);
}

Finally add this code to line 2549.

if ( isset($content_struct['custom_fields']) ) {
$this->set_custom_fields($post_ID, $content_struct['custom_fields']);
}

//insert taxonomies
if ( isset($content_struct['mt_taxonomy']) ) {
$this->set_new_taxonomy_tag($post_ID, $content_struct['mt_taxonomy']);
}

Now you are done editing the file. Save it. Now I give below an example php script which implements the above changes and add excerpts, tags, categories and custom keys to your post for remote publishing to wordpress through xmlrpc. You can use it to post successfully to your blog.

Example php script for remote publishing to wordpress through xmlrpc.

<?php include 'wp-includes/class-IXR.php'; 
$rpcurl = new IXR_Client('http:/www.example.com/xmlrpc.php');   //example.com=your blog 
url $username ="admin";                                 //  your blog login username 
$password = "password";                           //your blog password 
$content['title'] = "This is an example post title";         //put your post title here 
$content['categories'] = array('categoryname1,categoryname2');      //put the categories here
$content['mt_keywords'] = "tag1,tag2,tag3";           //put the tags here 
$content['mt_excerpt'] = "This is an example excerpt";        //put the excerpt here 
$content['description'] = "Here goes your post content"; 
//Add custom fields with keys and values 
$content['mt_taxonomy'] = array(array( 'Key1' => 'Value1, 'Key2' => 'Value2', 'Key3' =>'Value3')); 
if (!$rpcurl->query('metaWeblog.newPost','', $username, $password, $content, true)) 
{ die('An error occurred - '.$rpcurl->getErrorCode().":".$rpcurl->getErrorMessage()); } ?> 

Tags: , , , , , , , , , , , , ,

4 Responses to "How to add excerpt, tags, categories and custom fields to wordpress posts with remote publishing by xmlrpc"

Leave a Comment