WordPress XMLRPC- Publishing a page using metaWeblog.newPost
With wordpress XMLRPC, you can write your own client to blog from your desktop. metaWeblog.newPost takes six parameters
- Blog ID
- Username
- Password
- Content of the Page
- Post Type
- To Publish or not
The fifth parameter (Post type) can be used to publish a page. If this parameter is not set, a blog post is taken as the default parameter.But if it is set to ‘page’, a page will be published. The sixth 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
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 the ID of the post.
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.newPost request which takes on parameters
blogid,
username,
password
content
post_type
to publish or not*/
/*The title of your page*/
$title = "Sample Page";
/*The contents of your page*/
$description = "This is a sample page.";
/*Forming the content of page*/
$content['title'] = $title;
$content['post_type'] = 'page'; /*This is a page*/
$content['description'] = $description;
$content['wp_slug'] = "sample-page";
/*Whether the post has to be published*/
$toPublish = true;
$request = xmlrpc_encode_request("metaWeblog.newPost",
array(1,$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.newPost.php
The output will be something like this. Note the post ID.
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<string>44</string>
</value>
</param>
</params>
</methodResponse>
Executing the above program multiple times will create multiple posts
Tags: metaWeblog.newPost, page publication, WordPress, wordpress page publication, wordpress xmlrpc
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