How to add a <timestamp> tag to wordpress' XML-RPC newPost() function

Warning: Backup every file you want to edit, so you can recover the original status.

Information: If you don't know what XML-RPC is, you won't need anything on this page!

I introduced a new XML-RPC function called wp.newPost(). It works exactly like the MoveableType Syntax, but adds the possibility to use a <timestamp> tag in the post-content.
Since now you only could use <title> and <categories>, now you can use <timestamp> too. The timestamp uses the ISO 8601 format (eg. 2004-02-12T15:19:21+00:00) to specify the publish time.

Download the modified files to benefit from the new timestamp function.

I modified these two files:

In xmlrpc.php I added this code to the $this->methods array:

// Wordpress API (Custom) 'wp.newPost' => 'this:wp_newPost',

Then I added the new wp.newPost function:

/** * Wordpress custom function: wp.newPost * * This custom function supports a timestamp tag in the post-text in the ISO 8601 date format! */ function wp_newPost($args) { global $wpdb; $this->escape($args); $blog_ID = $args[0]; /* though we don't use it yet */ $user_login = $args[1]; $user_pass = $args[2]; $content = $args[3]; $publish = $args[4]; if (!$this->login_pass_ok($user_login, $user_pass)) { return $this->error; } $cap = ($publish) ? 'publish_posts' : 'edit_posts'; $user = set_current_user(0, $user_login); if (!current_user_can($cap)) return new IXR_Error(401, 'Sorry, you can not post on this weblog or category.'); $post_status = ($publish) ? 'publish' : 'draft'; $post_author = $user->ID; $post_title = xmlrpc_getposttitle($content); $post_category = xmlrpc_getpostcategory($content); $post_date = gmdate('Y-m-d H:i:s', (xmlrpc_getposttimestamp($content) + (get_settings('gmt_offset') * 3600))); $post_date_gmt = gmdate('Y-m-d H:i:s', xmlrpc_getposttimestamp($content)); $post_content = xmlrpc_removepostdata($content); $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status'); $post_ID = wp_insert_post($post_data); if (!$post_ID) { return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.'); } logIO('O', "Posted ! ID: $post_ID"); return $post_ID; }

In wp-includes/functions.php I added this function:

/** * Custom function: extracts the timestamp out of the content */ function xmlrpc_getposttimestamp($content) { $post_default_timestamp = time(); if ( preg_match('/<timestamp>(.+?)<\/timestamp>/is', $content, $matchcat) ) { $post_timestamp = trim($matchcat[1]); $post_timestamp = strtotime($post_timestamp); } else { $post_timestamp = $post_default_timestamp; } return $post_timestamp; }

Then I modified this function:

// modiefied to erase the timestamp function xmlrpc_removepostdata($content) { $content = preg_replace('/<title>(.+?)<\/title>/si', '', $content); $content = preg_replace('/<category>(.+?)<\/category>/si', '', $content); $content = preg_replace('/<timestamp>(.+?)<\/timestamp>/si', '', $content); $content = trim($content); return $content; }

Remember, that you need a client that supports the wp.newPost() function. Since I have created this function I don't think that there is any working client preconfigured for this function out there!

Here is an example text for a new post that has a timestamp tag:

<title>Custom Timestamp</title>
This post has a custom timestamp
<timestamp>2019-03-27T00:10:37+01:00</timestamp>

© anty