WordPress XML-RPC Patch For metWeblog newPost() Method

|

I have been doing some work with the XML-RPC functionality of WordPress and ran into a problem with the metWeblog.newPost() method. The problem? If you try to post a dateCreated value you actually get an error. At first I thought it was the format of the date/time that I was trying to post. So of course I tried about a googolplex combinations of formats, but always received the same error.

My next step was to investigate the code and find the line that was throwing the error. When I found the line of code the problem was only too obvious. The code block that caused the problem is as follows:

// Do some timestamp voodoo
$dateCreatedd = $content_struct['dateCreated'];
if (!empty($dateCreatedd)) {
    $dateCreated   = $dateCreatedd->getIso();
    $post_date     = get_date_from_gmt(
                       iso8601_to_datetime($dateCreated)
                     );
    $post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
} else {
    $post_date     = current_time('mysql');
    $post_date_gmt = current_time('mysql', 1);
}

Do you see the problem? No? The first line retrieves the dateCreated value that you passed into the method. This is a string. If the string is not empty then the next line (inside the if) calls the getIso() method on it… on a string. See the problem now? A quick search through the WordPress code found the getIso() method inside the IXR_Date class. This is obviously what they were going for so I just needed to cast our string date to an IXR_Date and that should fix it. This gave me the following:

// Do some timestamp voodoo
$dateCreatedd = $content_struct['dateCreated'];
if (!empty($dateCreatedd)) {
    $dateIXR       = new IXR_Date($dateCreatedd);
    $dateCreated   = $dateIXR->getIso();
    $post_date     = get_date_from_gmt(
                       iso8601_to_datetime($dateCreated)
                     );
    $post_date_gmt = iso8601_to_datetime($dateCreated. "Z", GMT);
} else {
    $post_date     = current_time('mysql');
    $post_date_gmt = current_time('mysql', 1);
}

A quick run of my test program confirmed that the solution was valid. So if you are using WordPress and would like to call the metaWeblog.newPost() method with a dateCreated value you can just modify your xmlrpc.php file with the code from above. Don’t want to search in the code? Oh, alright. Here are two patch files to help you out. One is for WordPress 2.1 and the other is for WordPress 2.2. To apply them just navigate to your WordPress installation directory. You should see the xmlrpc.php file. Upload/copy/whatever the patch file to this directory then issue this command:

patch -p0 < xmlrpc.2.x.patch

The x should be either 1 or 2 depending on the version of the patch you downloaded. Enjoy! 🙂

Patch For WordPress 2.1

Patch For WordPress 2.2