Discover in this tutorial how to clone a post to another WordPress installation using the REST API.
In this tutorial you are going to learn how you can use the REST API so that when a post is created on web1, another one like it is generated on web2.
Let’s see how to achieve this:
Instructions to clone posts from one WordPress installation to another using the REST API
1. Create a password for the REST API
The first thing you need to do is to create a new application password in the WordPress installation where you want the content (web2) of the original post (web1) to be sent.
To do this go, on web2, to Users > Profile and scroll down to the “Application Passwords” section.
Then fill in the new password name field (it can be anything) and click on “Add a new application password”.
Once done, the password name and password will be displayed. Be sure to copy it and keep it safe because it will not be displayed again.
2. Send the content and details of the post to another WordPress using the REST API
To get the information about the latest posts you are going to use the REST API endpoint that gives you access to that information.
Add the following code at the end of your web1 functions.php or functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
With this code you use the transition_post_status
hook with a conditional so that the content (and all the data you decide) is sent to the other WordPress installation when it is scheduled to publish on the same date.
But you can change this to do it on publish, just change $new_status == 'future'
to $new_status == 'publish'
.
In the first part of the snippet you define the variables:
$username
= WordPressusername
.$password
= Application password obtained in the previous step.$website_url
= The URL of web2.$request_url
= The URL where the request is made. In this case it is the concatenation:$website_url . '/wp-json/wp/v2/posts/'
which accesses the endpoint for the REST API entries.
Remember to change the value of the following variables to those of your case.
Then you use the wp_remote_request
function to get the endpoint for inputs and with the POST
method you pass the input data. In this case:
'title' =
Title 'status'
= Status (in this case draft) 'content'
= Content'date'
= Date
But you can add more to customize it to your liking. These are all the details you can send using the POST
method to the input endpoint:
date
date_gmt
slug
status
password
title
content
author
excerpt
featured_media
comment_status
ping_status
formart
meta
sticky
template
categories
tags
You have more information about it on official WordPress developer page.
If you have any questions or suggestions you can leave them in the comments.
Conclusions
Now you know how to use the REST API to clone posts from one WordPress installation to another.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉