Create a Post with WordPress REST API from Remote Website
WordPress REST API Authentication
Some time ago we had to use some external plugins in order to create an authentication with WordPress REST API from another website. One of those plugins was Application Passwords.
But super-good news are that this plugin has been merged into WordPress Core version 5.6, and you do not have to install it anymore.
Now let’s get a pair of Login and Password for the REST API examples below.
Login – is the username. You can get a password if go to the bottom of profile settings page.

misha
, password is 1HEu PFKe dnqM lr4j xDJX My63
, we will need it in examples below.If you’re working on localhost, do not forget to open wp-config.php
file and add the following constant there:
define( 'WP_ENVIRONMENT_TYPE', 'local' );
Create a Post with REST API – Example
Below you will find some PHP code examples written with WordPress HTTP API functions. I hope you don’t come up with the question of where to insert the code, because it is more like a developer tutorial and I suppose you already know that anyway.
But now let me remind you that we have two websites:
- The first website – is the website where the post should be created, the first website also is the one where we created our application password.
- The second website – is the website that will interact with the first one with REST API. It will have all the code.
Let’s create a simple draft right now!
$login = 'misha';
$password = '1HEu PFKe dnqM lr4j xDJX My63';
wp_remote_post(
'https://WEBSITE-DOMAIN/wp-json/wp/v2/posts',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "$login:$password" )
),
'body' => array(
'title' => 'My test',
'status' => 'draft',
)
)
);
Once you run the above code correctly, the post will appear on the Website 1.

Handle server response messages
Sometimes posts are not going to be created with the code you’re using. The good idea is to check error messages from the server.
$request = wp_remote_post( ... );
if( 'Created' !== wp_remote_retrieve_response_message( $request ) ) {
// ok we have some errors here
$body = json_decode( wp_remote_retrieve_body( $request ) );
print_r( $body );
}
For example you can get an error “Unknown username. Check again or try your email address” which occurs when you’re trying to use not a correct username in basic authentication of your REST API requests. I got it once when I tried to use application name instead.
On the other hand you can display success messages also:
$request = wp_remote_post( ... );
if( 'Created' === wp_remote_retrieve_response_message( $request ) ) {
$body = json_decode( wp_remote_retrieve_body( $request ) );
printf( 'The post %s has been created successfully', $body->title->rendered );
}
More parameters for REST API requests
In the above example we just used title
and status
to create a post with REST API. But obviously you will need more parameters for that, at least post content or maybe some meta data.
Of course all of them you can find in official WordPress documentation.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Awesome! Thanks for this quite nice tutorial. I have a question:
Why do I need “2 websites”? Can’t I run this script on the same WordPress installation that I want to automatize?
Regards.
Hi Aleksam,
Yes, as an example – Gutenberg works on REST API.
Hello again, Misha.
is there a way to get media files from a remote blog, using this authetication plugin?
Could you help me, please?
Regards!
Hi,
Sure, here is the tutorial. Just do not forget to pass
type
parameter which should beattachment
.Sweet! Thanks.
How can i add posts to a Custom Post Type via API?
Just pass
type
parameter in the first example here.Hello Misha,
Thanks for this post. I was wondering how can I do this using JS?
Thanks
Hey Moses,
Sorry, usually I remove comments with unformatted code, but at this time I just decided to remove the code only, because you’re asking a good question.
I didn’t have a chance to do this with JS yet, but maybe somebody from my website readers kindly answers your question.
Hey Misha, how can i update the category of my posts?
Hey Sam,
Did you try to pass
'categories' => array( 1, 5, 10 )
?Hi Misha,
great stuff, thanks!
Say I wanted to update a certain data field of all available products in WooCommerce, how would you approach that?
I know how to do this for one specific ID at a time, but to run a loop or so for certain products containing certain values?
Best,
Kjetil
This do solve my problem .
Is it possible to Update also Custom Filds while Updating a post? How?
Thanks
Yes, of course. This tutorial is about publishing with custom fields, but updating is kind of similar.
Hello.
Can you help with this problem?
I use this post for create custom PostType, but I get the error “Sorry, you are not allowed to create records from the person of this user.” What can this be connected with?
Was inattentive, problem solved
You should mention both sites need to have a wp instalation :-)