Create New Posts With Publishing Date in WordPress Using XML-RPC and PHP

To be able to add new posts to a WordPress blog I wanted to use XML-RPC interface with PHP.

First of all you need to enable the checkbox “Enable the WordPress, Movable Type, MetaWeblog and Blogger XML-RPC publishing protocols.” in the menu “Settings”/”Writing”.

Now I searched on the web for a ready-to-use solution as I was probably not the first one who wanted to accomplish that. And as expected there were also many PHP scripts doing exactly that, but with one problem:
I wanted to set also the publishing date manually with the PHP request and most scripts I’ve found did not even try to do that at all.

Few scripts in fact did try to set the publishing date, but the result was an error message similar to:
Fatal error: Call to a member function getIso() on a non-object in /var/www/wp-includes/class-wp-xmlrpc-server.php on line 2373

After reading some documents regarding this strange ISO 8601 date format that is needed there, I found a solution myself:

/**
 * Performs a XML-RPC to a WordPress blog and adds a new post.
 *
 * @param string $title Title of the new post.
 * @param string $body Body text of the new post.
 * @param string $rpcurl RPC-URL of the blog (normally xmlrpc.php in the root directory).
 * @param string $username User name used to add the new post.
 * @param string $password Password used to add the new post.
 * @param string $categories Comma separated list of categories (make sure they already exist in your blog).
 * @param string $keywords Comma separated list of keywords/tags (in contrast to the categories they don't need to exist already).
 * @param string $pubdate Publishing date in ISO 8601 (with date('c') in PHP; current time is used if empty).
 * @param string $draftonly True to save as draft only, False to publish directly (default).
 * @return string The result of the XML-RPC.
 */
function wpPostXMLRPC($title, $body, $rpcurl, $username, $password, $categories, $keywords = '', $pubdate = '', $draftonly = false) {
	$content = array(
		'title' => $title,
		'description' => $body,
		'mt_allow_comments' => 1,
		'mt_allow_pings' => 1,
		'post_type' => 'post',
		'date_created_gmt' => '%pubdate%', // Just as place holder here.
		'mt_keywords' => $keywords,
		'categories' => array($categories)
	);
	$params = array(0, $username, $password, $content, !$draftonly);

	$request = xmlrpc_encode_request('metaWeblog.newPost', $params);
	// Now we need to set the real publishing date:
	$request = str_replace('<string>%pubdate%</string>',
			       '<dateTime.iso8601>' . $pubdate . '</dateTime.iso8601>',
			       $request);

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_URL, $rpcurl);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 1);
	$results = curl_exec($ch);
	curl_close($ch);
	return $results;
}

As you can see I inserted the data structure needed for the correct ISO 8601 date format myself directly into the XML request. This is probably not the most elegant way, but it works perfectly. ;-)

Did you come across the same issue already yourself?

This post is also available in Deutsch.

18 thoughts on “Create New Posts With Publishing Date in WordPress Using XML-RPC and PHP

  1. Hi, thanks a lot for your code!!

    I search this from a long of time!!

    I have put your code in a .php page and I just at the top this line:
    $username = “mylogin”;
    $password = “mypassword”;
    $rpcurl = “http://www.mywebsiteurl.com/xmlrpc.php”;
    $body =”mypostcontent”;
    $title =”myposttitle”;

    And I upload the page with your code tu my ftp at in my wordpress root folder to test and it’s not working…

    Have you an idea for my problem ?

    Thanks!

    PS: Sorry form my english… I am french

  2. ok, thanks for your response:

    the complete code is:

    $title,
    ‘description’ => $body,
    ‘mt_allow_comments’ => 1,
    ‘mt_allow_pings’ => 1,
    ‘post_type’ => ‘post’,
    ‘date_created_gmt’ => ‘%pubdate%’, // Just as place holder here.
    ‘mt_keywords’ => $keywords,
    ‘categories’ => array($categories)
    );
    $params = array(0, $username, $password, $content, !$draftonly);

    $request = xmlrpc_encode_request(‘metaWeblog.newPost’, $params);
    // Now we need to set the real publishing date:
    $request = str_replace(‘%pubdate%’,
    ” . $pubdate . ”,
    $request);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
    }

    ?>

    the script is in the same folder to xmlrpc.php

    Do you see an error ?

  3. Oh sorry, pb with paste. the code:
    $username = “mylogin”;
    $password = “mypassword”;
    $rpcurl = “http://www.mywebsite.com/xmlrpc.php”;
    $body =”hello”;
    $title =”my contect text”;

    function wpPostXMLRPC($title, $body, $rpcurl, $username, $password, $categories, $keywords = ”, $pubdate = ”, $draftonly = false) {
    $content = array(
    ‘title’ => $title,
    ‘description’ => $body,
    ‘mt_allow_comments’ => 1,
    ‘mt_allow_pings’ => 1,
    ‘post_type’ => ‘post’,
    ‘date_created_gmt’ => ‘%pubdate%’, // Just as place holder here.
    ‘mt_keywords’ => $keywords,
    ‘categories’ => array($categories)
    );
    $params = array(0, $username, $password, $content, !$draftonly);

    $request = xmlrpc_encode_request(‘metaWeblog.newPost’, $params);
    // Now we need to set the real publishing date:
    $request = str_replace(‘%pubdate%’,
    ” . $pubdate . ”,
    $request);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
    }

    ?>

  4. Doesn’t work, all I get is this on all posts whatever I send as date: 1999/11/30

    example sample:
    function wpPostXMLRPC($title, $body, $rpcurl, $username, $password, $categories, $keywords = ”, $pubdate, $draftonly = false) {

    wpPostXMLRPC($title,$content,”http://www.example.com/xmlrpc.php”,”xxx”,”xxx”,$categories=array(1),”",date(‘c’),false);

    • Hello Per,

      first thanks for your comment!

      That’s strange, it worked fine – at least a the point in time when I wrote this post. Maybe there is an issue with a newer WordPress version: I’ll test it again as soon as I have some time for it.

      If somebody else has a similar problem with that, please comment here and also write your WordPress version that you’re using currently.

      Best regards
      Andreas

  5. Solved it. This is the correct time format 20100825T08:30:00Z (at least on my WordPress installation / server). Therefore, date(‘c’) doesn’t work in this case. Back to search and replace for dates :)

  6. Well done, very smart and thank for the script !
    After a big number of hours spent of this damned subject, I was not so far from the solution and was ready to start the work. Thanks god I founded your post before!
    Thank you again and viva internet :)

    ps: CURLOPT_TIMEOUT could be more than 1 if your wordpress blog is slow.
    For my test, I put 10.

  7. Hey,

    Thanks a lot for the script. I am having trouble with the date thing..

    Can you please suggest what code to use to get the supported time format?

    date(“c”); doesnt work with this.

    Thanks and Regards

    • Hello Mudit,

      you could try this:
      date(‘Ymd’, $yourdate) . ‘T’ . date(‘H:i:s’, $yourdate) . ‘Z’

      Best regards
      Andreas

Leave a Reply to Per Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>