WordPress XMLRPC – Posting Content From Outside WordPress Admin Panel
Many WordPress blog owners these days prefer to post their blog content from outside. This facilitates the site owners to post blog posts without even loging into their WordPress admin panel. WordPress supports XMLRPC which can take requests and perform specific operations i.e. ‘Add Post’, ‘Edit Post’, ‘View Recent Posts’ or ‘Save a draft’.
Lets start coding now, but before we go into any details lets ensure that our WordPress installation is allowing XMLRPC (default is turned off). Go to WordPress Admin => Settings => Writing and ensure the XML-RPC is enabled. Click save after checking the enable box. We are good and our WordPress is ready to take XML-RPC commands.
We will use IXR Library to incorporates both client and server classes, as it is designed to hide as much of the workings of XML-RPC from the user as possible. A key feature of the library is automatic type conversion from PHP types to XML-RPC types and vice-versa. This should enable developers to write web services with very little knowledge of the underlying XML-RPC standard.
Download IXR_Library.php.inc (rename it to IXR_Librabry.php.inc) or download the latest version from Incutio
Add a WordPress Blog Post via php
<?php require_once("IXR_Library.php.inc"); $client->debug = true; //Set it to false in Production Environment $title="Blog Title"; // $title variable will insert your blog title $body="Blog Content"; // $body will insert your blog content (article content) $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( '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) ); // Create the client object $client = new IXR_Client('Your Blog Path/xmlrpc.php'); $username = "USERNAME"; $password = "PASSWORD"; $params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false' // Run a query for PHP if (!$client->query('metaWeblog.newPost', $params)) { die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage()); } else echo "Article Posted Successfully"; ?>
Get Recent 10 WordPress Blog Posts via php
<?php set_time_limit(0); require_once("IXR_Library.php.inc"); $client->debug = true; // Set it to fase in Production Environment // Create the client object $client = new IXR_Client('Your Blog URL/xmlrpc.php'); $username = "Blog Admin/Editor/Author User Name"; $password = "PASSWORD"; $params = array(0,$username,$password,10); // Last Parameter tells how many posts to fetch // Run a query To Read Posts From Wordpress if (!$client->query('metaWeblog.getRecentPosts', $params)) { die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage()); } $myresponse = $client->getResponse(); $i=0; ?> <table border="1" cellpadding="0" cellspacing="2"> <tr> <td>Sr. No</td> <td>Date</td> <td>Author</td> <td>Post Id</td> <td>Title</td> <td>Categories</td> <td>Tags</td> <td>Status</td> </tr> <?php //foreach ($myresponse as $key => $value) foreach ($myresponse as $res) { if($res['post_status']!="draft"){ //$times = new IXR_Date(); ?> <tr> <td><?php echo $i+1; ?></td> <td> <?php $object = $res['dateCreated']; echo $object->day."-".$object->month."-".$object->year." ".$object->hour.":".$object->minute; ?></td> <td><?php echo $res['wp_author_display_name']; ?></td> <td><?php echo $res['postid']; ?></td> <td><a href="<?php echo $res['permaLink']; ?>"><?php echo $res['title']; ?></a></td> <td> <?php echo implode($res['categories'],", ") ?> </td> <td><?php echo $res['mt_keywords']; ?></td> <td><?php echo $res['post_status']; ?></td> </tr> <?php $i++; } } ?> </table>
Edit WordPress Blog Post via php
<?php // Update a Selected Post $title= "Updated Title"; // The Title of the Post $bodycontent="Updated Blog Article Content"; // Article Content $categoriesu= array('Category1', 'Category2'); // Pre Existing Categories - Updated $tagsu="tag1, tag2, $tag3"; // Updated Tags $content = array('title'=>$title, 'description'=>$bodycontent,'categories'=>$categoriesu,'mt_keywords'=>$tagsu); $params = array(163,$username,$password,$content,1); // First Parameter is mandatory Post Id (get your post id via Recent Blogs entries) // Run a query for PHP if (!$client->query('metaWeblog.editPost', $params)) { die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage()); } ?>
I have made a centralized Word-press control panel where i am posting blog posts in more than 20 different blogs using the same admin area. There is no need to log into 20 blog accounts to post my content. I would love to hear your thoughts on this. Please leave me a comment and let me know. Don’t forget to subscribe our RSS to receive latest updates.our RSS to receive latest updates.
Mind Tree 






























