WordPress XMLRPC- metaWeblog.editPost
With wordpress XMLRPC, you can write your own client to blog from your desktop. metaWeblog.newPost takes five parameters
- Post ID
- Username
- Password
- Content of the Post
- To Publish or not
The first parameter is the ID of the post to be edited. The fourth parameter contains the content of the post (edited post).The fifth parameter is a boolean value, true or false, whether to publish the post or not
The fourth argument, contents of a blog must have the following attributes
- Title
- Date in ISO8601 format
- Description
- Categories
- Tags
You can change the title, date, description, categories or tags. Here we demonstrate how to change the contents
Note that the categories must be an existing one. You cannot create a new category with this function. The return value of this function is a boolean value whether the post is posted or not. Let’s first try with an invalid post ID
Let’s see a simple PHP program
<?php
$BLOGURL = "http://localhost/wordpress";
$USERNAME = "admin";
$PASSWORD = "check";
function get_response($URL, $context) {
if(!function_exists('curl_init')) {
die ("Curl PHP package not installed\n");
}
/*Initializing CURL*/
$curlHandle = curl_init();
/*The URL to be downloaded is set*/
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, false);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);
/*Now execute the CURL, download the URL specified*/
$response = curl_exec($curlHandle);
return $response;
}
/*Creating the metaWeblog.editPost request which takes on five parameters
postid: the post to be edited,
username,
password
contents
to publish or not*/
/*The id of the post to be edited*/
$postid = 140;
/*The title of your post*/
$title = "Sample Post Title";
/*The contents of your post (The new contents or the edited post)*/
$description = "This is the contents of the edited post.";
/*Forming the content of blog post*/
$content['title'] = $title;
$content['description'] = $description;
$content['categories'] = array("Blogging", "category1");
$content['mt_keywords'] = array("Blogging", "tag1");
$content['wp_slug'] = "sample-post";
/*Whether the post has to be published*/
$toPublish = true;
$request = xmlrpc_encode_request("metaWeblog.editPost",
array($postid,$USERNAME, $PASSWORD, $content, $toPublish));
/*Making the request to wordpress XMLRPC of your blog*/
$xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
$response = xmlrpc_decode($xmlresponse);
/*Printing the response on to the console*/
print_r($response);
echo "\n";
?>
To execute the program
$ php metaWeblog.editPost.php
The output will be something like this. Note the post ID.
<?xml version="1.0"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>404</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Invalid post ID.</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>
As you can see the error response: Invalid post ID. Let’s now try the same program with a valid post ID.
$postid = 40;
The above number corresponds to a post already posted
Executing the above program with the new post ID
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<boolean>1</boolean>
</value>
</param>
</params>
</methodResponse>
See the boolean value set to true, which corresponds to the post successfully edited.
Tags: metaWeblog.editPost, wordpress editing post, wordpress xmlrpc, wordpress XMLRPC Example, wordpress xmlrpc post editing
Comments:
On Facebook
Translate









Search
Custom SearchRecent Comments
- Ash on log4cpp::PatternLayout – Available log4cpp log formats
- Joys of Programming on Curl / libCurl Tutorials with Examples
- Malik89 on Curl / libCurl Tutorials with Examples
- Matt on How to install java library for jogl in Ubuntu/Linux?
- Gokulnath on How to install Readline/libreadline in Ubuntu?
On Twitter
Tutorials
Twitter
- And you thought Eclipse is Just for Java Development. You can also use Eclipse for C/C++ development http://t.co/swtOLbkr 3 months ago
- For the C fans here, let's see a simple question. Is there any difference between func() and func(void)? http://t.co/ASTsWVc9 3 months ago
- ISO C++11 released. http://t.co/DyJ0rSa1 3 months ago
- RT @newsycombinator: IE team sends a cupcake for FireFox 5 http://j.mp/lS7pGF 7 months ago
- RT @newsycombinator: Voice search enabled on Google homepage (Chrome) http://j.mp/m5KT8c 7 months ago
Categories
this is very helpful and clear document.
Thanks ruby that you found this post useful
What a great series of articles! I’ve been struggling to find this information in a clear and concise format, and you’ve done a brilliant job!
@David Thanks that you found this article useful. You can also look at the other Tutorials
awesome! thanks for the helpful instructions.