3

I want to achieve the following -

I have 2 WordPress sites hosted on different servers. Let's call A as sender site and B as receiver site. Whenever I add/update/delete any post in A, it should be reflected as it is in B. Not only post data but the taxonomies and custom fields should also be updated properly in B.

I checked WordPress REST API but I do not prefer it since it just keep a JSON feed on A for which I can send pull request from B. This is fine with posts pulling but not suitable for update and delete. Also, suppose, if today there are 10 blogs, tomorrow there can be 100 blogs and so on. So, as per my understanding it's not best fit for my requirement.

Another approach that I probably looking to go with is adding a functionality in A to send remote request to B and B should have endpoints ready.

E.g. If I create an endpoint at B site say 'update_post' and send request to it from A on a hook. Like on save post action, send a request to 'update_post' endpoint available on B and then B will create post with the post data received from A. The ID of the post would be different in B than A for that post, so I can save the ID got from A in the post meta of the post created in B and then on this relationship basis, handle activities like update, delete etc in future. On update, I also have to take care of postmeta, category/tags, featured image updates.

What I want for now is to create endpoints -

If I create an endpoint like http://example.com/insert_post then I have to create an external PHP file where WordPress functions are not available and I think making use of wp-load.php to load WordPress isn't a good idea. So, I want to achieve this in WordPress way only.

I researched and found this function http://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint. I also got many articles regarding it but at all the places, I see, they say that we can create endpoint to post, pages, comments etc in json format. I am not sure whether and how it can be helpful in my scenario. I just want this -

  1. Create a custom endpoint
  2. Make WordPress functionality available in a proper way

Any help would be greatly appreciated. Thanks in advance !!!

1
  • What makes you think the REST API does only support reading access? You can create posts and even update existing posts. I agree that synchronously pushing posts will not scale. But thats a problem on any network based method. If you decide to work on a push-based solution you should consider a proper queuing system.
    – David
    Commented Aug 29, 2015 at 15:10

1 Answer 1

2

Have you tried codex.wordpress.org/XML-RPC_Support.

This code works, in conjunction with the IXR library found at incutio:

include('IXR_Library.php');

$usr = 'theusername';
$pwd = 'thepassword';
$xmlrpc = 'http://not-therealurl.com/xmlrpc.php';
$client = new IXR_Client($xmlrpc);

$client -> debug = true; //optional but useful

$params = array(
    'post_type' => 'post',
    'post_status' => 'draft',
    'post_title' => 'Test Post',
    'post_author' => 4,
    'post_excerpt' => 'This is my test excerpt',
    'post_content' => 'This is my test post. Now its longer than the excerpt.'
);

$res = $client -> query('wp.newPost',1, $usr, $pwd, $params); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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