Doing this automatically seems a bit complicated, but I think it's possible. The solution I'm suggesting has not been tested, but in theory I think it should work. I will call from w1
the WordPress in which you want to create the post, and from w2
the WordPress that will receive the posts, once they are created in the first one.
Maintain integrity between the two sites
This one I believe should be step zero, but anyway. If you have some sort of customization in w1
posts, such as meta data
, custom fields
and things like, and do not have at least the same structure in w2
, the post that you are wanting to duplicate will not be duplicated 100% correctly. So make sure w2
has at least all the properties you set in w1
.
Create a action hook for posting in w1
WP has a series of predefined actions , and tying some more functionality to them is taken as creating a hook. The action we're going to use is the publish_post . From codex:
publish_post is an action triggered whenever a post is published, or if it is edited and the status is changed to publish.
That is, every time you post a post, something (that you define) will happen. In terms of code, this is the skeleton you put in functions.php
:
add_action('publish_post', 'replicar_post_em_w2');
function replicar_post_em_w2(){
# code...
}
Basically, replicar_posts_em_w2()
will be called at the time of publication. I'm going back to this method soon.
Create a posts insertion interface in w2
There is more than one way you can do this, but one that I particularly like is using the JSON API (From this point, my entire answer is based on this plugin). It provides query methods - which return a JSON
based on URL
and its parameters - as well as methods of creating posts (which interests us), which works basically the same way. You call a URL
of type http://w2.com/api/create_post/?nonce=123abc&content=blablabla
, and it creates the post for you. Speaking specifically of this plugin, there is a method called get_nonce()
that should be used to generate nonce
, which is a required parameter for the create_post()
method. Read about the details of the plugin here .
Call, within the action hook , your insertion method in w2
Now that you have (or should have) ready, and tested, the post insertion structure in w2
, it's time to build the replication method itself. Since you are going to work with post
, you should provide it as a parameter for this method. Inside it, you'll extract what you need from the post you just created in w1
, mount a query string using this method, and call the URL
insertion of w2
to complete the process. In a very simple way, your code would be:
function replicar_post_em_w2($post){
$url_de_insercao = 'http://w2.com/api/create_post/?';
$titulo = $post->title;
$couteudo = $post->content;
$nonce = '123abc';
#... E por aí vai
$query_params = array(
'nonce' => $nonce,
'title' => $title,
'content' => $conteudo
);
$url_de_insercao .= http_build_query($query_params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_de_insercao);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
Note that I used cURL
to call the insert method in w2
. Note also that the value of nonce
was arbitrary. You can use cURL
to make the request in w2
to get the correct value. The list of parameters for all methods of the plugin can be found at documentation .
Finally, since your method is now given a parameter, you have to change the add_action
to:
add_action('publish_post', 'replicar_post_em_w2', 10, 1);
Where 10 is the priority, and 1 is the number of arguments of replicar_post_em_w2
.
I believe that if you follow these steps, you will achieve your goal. Of course you'll have a good debug job ahead of you, and post insertion can be a bit boring, mainly because of nonce
. Constructing query
can be a bit complex, too, and if you're working with custom posts
, insertion may be a bit tricky. Anyway, the theory is this. I hope I have helped.