Is there a way to know if a post has been published through XML-RPC?

It’s all in the title, I’m looking for a way to know if a given post was published through XML-RPC, versus published by hand in the WP admin.

Pseudo code :

if( !wpse_from_xmlrpc( $post -> ID ) {
    // Doesn't come from XMLRPC
} else {
    // Comes from XMLRPC 
}

1 Answer
1

You could use a custom field for a post which is saved via XMLRPC by using the action hook xmlrpc_publish_post. wpse_from_xmlrpc() could than check this custom field.

<?php
    add_action( 'xmlrpc_publish_post', 'add_xmlrpc_postmeta' );
    function add_xmlrpc_postmeta( $post_id ){
        update_post_meta( $post_id, 'send-by-xmlrpc', 1 );
    }

    function wpse_from_xmlrpc( $post_id ){
        $xmlrpc = get_post_meta( $post_id, 'send-by-xmlrpc', true );
        if( $xmlrpc == 1 )
            return true;
        return false;
    }
?>

More information on this hook can be found in wp-includes/post.php

Leave a Comment