XMLRPC Avoid duplicate content

Since you add a reference to my previous answer, let me share how I tested it:

Setup on site A – XML-RPC Client

include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );

$client = new WP_HTTP_IXR_CLIENT( 'http://example.tld/xmlrpc.php' ); // <-- Change!
$client->debug = true;

$result = $client->query( 
    'wp.newPost', 
    [
        0,
        "username",    //<-- Change!
        "password",    //<-- Change!
        [
            'post_status'  => 'draft',
            'post_title'   => 'xml-rpc testing',
            'post_content' => 'hello xml-rpc! Random: ' . rand( 0, 999 ),
        ]
    ]
);

where you have to modify the path, username and password to your needs.

If I recall correctly, this great article by Eric Mann helped me regarding the client setup code, when I tested my plugin last year.

Setup on site B – XML-RPC Server

Here we add the following plugin:

<?php
/**
 * Plugin Name: Avoid XML-RPC Post Title Duplication
 * Description: Prevent duplicate posts when doing wp.newPost via XML-RPC
 * Plugin URI:  http://wordpress.stackexchange.com/a/157261/26350
 */

add_action ('xmlrpc_call', 'wpse_xmlrpc_call' );  /////

function wpse_xmlrpc_call( $method )
{
    if( 'wp.newPost' === $method )
        add_filter( 'xmlrpc_wp_insert_post_data', 'wpse_xmlrpc_wp_insert_post_data' );
}////

function wpse_xmlrpc_wp_insert_post_data( $post_data )
{
    // Check if the post title exists:
    $tmp = get_page_by_title( 
        $post_data['post_title'], 
        OBJECT, 
        $post_data['post_type'] 
    );

    // Go from 'insert' to 'update' mode within wp_insert_post():
    if( is_object ( $tmp ) )
        $post_data['ID'] = $tmp->ID; 

    return $post_data;  
}

Tests

Before activating our plugin:

If client A creates three posts with the same title, but different content, then they will show up like this on site B:

before

Here we see that those three posts are all created on the server B as new posts.

After activating our plugin:

Now if client A creates a post, then it will show up on server B like this:

after2

Then client A creates another post, with the same title, but different content. Now the previous post is modified:

after3

The post list will show up like this:

after1

so we have avoided post duplication.

Notes

Make sure site B has XML-RPC enabled.

Leave a Comment