2

I want to avoid duplicate post content via xmlrpc. So:

if post exists nothing happens... . if does not exists data insertion occurs... .

The method for checking would be post title or post content. I saw this solution but it seems not to work.

4
+50

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.

  • Thank you. It works! a couple of question. if I wanted to no nothing on duplication (do not update..) what should I do? If there is 200 duplicate requests per minute do I have to be concerned about it's performance? – David Peterson Oct 19 '15 at 12:39
  • Glad to hear it worked for you. To avoid the duplication, I add the corresponding value to the ID field. So without that part, we would get a duplication. Regarding 200 req/min that depends on the server setup and number of posts. You should test your install for that kind of updates. I'm not sure if xml-rpc setup in WordPress supports bulk inserts, but I just want to mention that there are some ways to improve wp_insert_post, e.g. here but that's most likely not relevant here where there are only single inserts each time. @DavidPeterson – birgire Oct 19 '15 at 12:55
  • but I guess by to do nothing, you mean to only allow modifications x times a minute? You might then check when the post was last modified and throw an IXR_Error error if it's too early. @DavidPeterson – birgire Oct 19 '15 at 13:17
  • I just realized that it doesn't work... . I created a log file per each line and noticed that thr (add_filter( 'xmlrpc_wp_insert_post_data', 'wpse_xmlrpc_wp_insert_post_data' );) never goes to the plugin function ( wpse_xmlrpc_wp_insert_post_data)... . notice that there were some mistyping in your code and I edited the answer... . – David Peterson Oct 19 '15 at 15:18
  • Thanks for the edit and sorry for the silly syntax errors, they came from my previous answer that I copied into my answer here. I just checked the edited code, and it's running fine on my install ;-) @DavidPeterson – birgire Oct 19 '15 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.