WordPress has supported MetaWeblog API that Allows external programs to get and set the text and attributes of weblog posts.To create a new posting remotely, requiring metaWeblog.newPost as XML-RPC API communication protocol. Read MetaWeblog API official site to the documentation
To be able to communicate with metaWeblog.newPost, a class IXR_Library required. Why do we use IXR_Library class? I have explained in the post Simple wordpress xmlrpc with IXR_Library Class.
How to Using metaWeblog.newPost
metaWeblog.newPost is use to create new post. For parameters used in metaWeblog.newPost you can read it from wordpress.org
Okay, lets begin my metaWeblog.newPost and IXR_Library tutorial
1. You need download IXR_Library here.
2. Create new php file as new_post.php and copy the code below
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 | include "IXR_Library.php"; $XmlRpc_result = null; $url= 'http://yoursite.com/xmlrpc.php'; // Your XMLRPC Url $XmlRpc_client = new IXR_Client ($url); $date = new IXR_Date(strtotime('2015-02-23 18:57:33') ); // writing publish date $encoding='UTF-8'; $title="New post with metaWeblog.newPost and IXR_Library by seegatesite"; // your post title $body="Hello world, this is my metaWeblog.newPost and IXR_Library first content"; // the article content $category="category1, category2"; // Post category can be seperated by comma seperated. Ensure that these categories exists in your site. $keywords="keyword1, keyword2, keyword3"; // This is tag post $customfields=array('key'=>'My Xmlrpc', 'value'=>'metaWeblog.newPost'); // Custom field $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) , 'date_created_gmt' => $date ); $params = array(1,'your-username','your password',$content,true); // set true if you need to publish post, set false if you need set your post as draft try{ $XmlRpc_result = $XmlRpc_client->query( 'metaWeblog.newPost',$params ); $data = $XmlRpc_client->getResponse(); print_r( $data ); } catch (Exception $e){ var_dump ( $e->getMessage ()); } |
Explanation
1 | $date = new IXR_Date(strtotime('2015-02-23 18:57:33') ); |
the code above is one of the advantages of using IXR_Library. Using IXR_Date class allows us to set the post time.You can do the scheduling post as you want.
1 | $params = array(1,'your-username','your password',$content,true); |
Syntax above is how to set parameter. Set true if you want publish the post, set false if you need set draft to your post status.
1 2 3 | $XmlRpc_result = $XmlRpc_client->query( 'metaWeblog.newPost',$params ); |
To use metaweblog.newpost use syntax $XmlRpc_client->query().
Use $data = $XmlRpc_client->getResponse() to get return value, the result of return value is postid as string.
Finish :), You can read my another XMLRPC article here or maybe you can combine metaWeblog.newPost with amazon script to create auto post article from amazon .
Leave a Reply