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.

create application password for WordPress REST API authentication
So, login is 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:

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.

WordPress REST API create post

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

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

Follow me on X