XMLRPC – Programmatically post on WordPress with an attach image to post in WordPress

This is how I make it!
Shared on StackOverflow


$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_status' => 'draft', // http://codex.wordpress.org/Post_Status
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'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('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'');

$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)),
if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) {
Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
'post_thumbnail' => $media['id']
if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) {
Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'

2 thoughts on “XMLRPC – Programmatically post on WordPress with an attach image to post in WordPress

  1. I have integrated xmlrpc in php , now i can able to post in word-press through php xmlrpc API , I have installed the custom post plugin & created the custom posts in wordpress, When i try to post in the custom posts, i am getting the error as invalid post type. The custom post slug name is custompost. is it possible to create the custom post in XmlRPc php?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.