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.
XML-RPC is by default enabled in WordPress 3.5+ that’s cool. On the other hand what if you are not using XMLRPC you can anytime disable it like this. Just place the following line of code in your themes functions.php
file and you are all set.
1 |
add_filter('xmlrpc_enabled', '__return_false'); |
But for the sake of our article, i assume you will not disable XMLRPC.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?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 immediately, 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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()); } ?> |
Posting Images and Marking Featured Via WordPress XMLRPC
The new wordpress thumbnail feature is so great, now all you have to do is upload an image and mark it as featured. If this is something you are looking for via WordPress XMLRPC then look at the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$myFile = "myimage.jpg"; $fh = fopen($myFile, 'r'); $fs = filesize($myFile); $theData = fread($fh, $fs); fclose($fh); $client = new IXR_Client('Your Blog Path/xmlrpc.php'); $username = "USERNAME"; $password = "PASSWORD"; $client->debug = false; $params = array('name' => 'myimage.jpg', 'type' => 'image/jpg', 'bits' => new IXR_Base64($theData), 'overwrite' => true); $res = $client->query('wp.uploadFile',1, $username, $password, $params); $clientResponse = $client->getResponse(); //echo URL & image id $thumbnail_image = $clientResponse['url']; $thumbnail_id = $clientResponse['id']; |
Code Explanation
Here is how it works
1. We are assuming you have already uploaded the file in your application, all you need to do is read the image file by this code
1 2 3 4 5 |
$myFile = "myimage.jpg"; $fh = fopen($myFile, 'r'); $fs = filesize($myFile); $theData = fread($fh, $fs); fclose($fh); |
2. Connect to your WordPress XMLRPC
1 2 3 |
$client = new IXR_Client('Your Blog Path/xmlrpc.php'); $username = "USERNAME"; $password = "PASSWORD"; |
3. Now, upload the file to wordpress via XMLRPC
1 2 3 4 5 6 7 |
$client->debug = false; $params = array('name' => 'myimage.jpg', 'type' => 'image/jpg', 'bits' => new IXR_Base64($theData), 'overwrite' => true); $res = $client->query('wp.uploadFile',1, $username, $password, $params); $clientResponse = $client->getResponse(); $thumbnail_image = $clientResponse['url']; $thumbnail_id = $clientResponse['id']; |
Finally lets see the full code example, how this uploaded image is marked as featured in your WordPress site.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
$myFile = "myimage.jpg"; $fh = fopen($myFile, 'r'); $fs = filesize($myFile); $theData = fread($fh, $fs); fclose($fh); $client = new IXR_Client('Your Blog Path/xmlrpc.php'); $username = "USERNAME"; $password = "PASSWORD"; $client->debug = false; $params = array('name' => 'myimage.jpg', 'type' => 'image/jpg', 'bits' => new IXR_Base64($theData), 'overwrite' => true); $res = $client->query('wp.uploadFile',1, $username, $password, $params); $clientResponse = $client->getResponse(); $thumbnail_image = $clientResponse['url']; $thumbnail_id = $clientResponse['id']; //This thumbnail id is provided in post content array $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), 'wp_post_thumbnail' => $thumbnail_id, //thumbnail id of the picture, this will be marked as featured. ); // 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"; ?> |
Based on the code given below you can define your own logic of posting images in your article. Suppose you want to show the uploaded image within your content, you can append the $thumbnail_image
within your $body
object e.g.
1 |
$body .="<img src='".$thumbnail_image."'>" |
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.