WordPress XMLRPC – Posting Content From Outside WordPress Admin Panel


wordpress xmlrpc 295x300 WordPress XMLRPC – Posting Content From Outside WordPress Admin PanelMany 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.


Related posts

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • Code Seo
    Sorry for my englis, i'm French but great article to understand the way to multi post.

    If you want i've developped a little class for Wordpress and XMLRPC: http://www.scripts-marketing.com/lfe-une-classe-et-un-outil-pour-faire-vivre-votre-ferme/

    With anonymous comment posting because a default comment is for the admin :)
  • Bergmp
    Nice script friend, thanks, I have a simple question: how do I include images into post, and how to align them?
  • Gulshan Saini
    Hello Ashish,

    I am facing problem while posting to multiple categories

    Thanks
    Gulshan
  • Gulshan,

    Wordpress will take categories as an array e.g. $category="category1, category2"; you can see this at the top of my code. Keep in mind that when you associate a post in multiple categories Wordpress will take the category with lowest id while assigning Permalink.

    What's the exact problem you are facing?
  • Hello Ashish !! You've done the brilliant thing! I'm searching for this info around half day but no one seems pretty easy for me as yours.
  • I hope the code worked perfectly for you. I have programmed the same code for blogger, posterous and tumblr as well.
  • Wirat O
    Great :-)

    Ashish, about creating new post via php script. Is there anyway to choose post-url I want?

    Example, my new post title is Best WordPress Plugin I Recommend and I want post-url to be best-wordpress-plugin-i-recommended

    Thanks
  • I haven't used and thought of sending post-URL as a parameter. To my knowledge Wordpress automatically creates a new post URL (permalink) from the post title. Can you tell me your exact requirement so i can think deeper in this.
  • Wirat O
    What I really want to do is to make a connection of all posts I have created similar to this picture http://download.smilesquare.com/linkwheel.gif
  • Look at get recent 10 posts code and it will give you the last 10 blog posts. Just find out the permalink and use in your own logic. I don't think you need to send blog post URL specifically.

    You can see all the blog posts from all the blogs (blog1-blog10) and then fetch and show the desired data in your money site.
  • Wirat O
    Thank you Ashish for your suggestion! I have implemented some basic algorithms and now it's working as I want.
  • Superb!!! I've being looking for this for around 15 days!!! and this is the only code I've found that works. Thanks, you saved my day :)
blog comments powered by Disqus