WordPress REST API – Create, Update or Delete posts using Basic Auth and HTTP API


You must use a plugin to use WordPress REST API with Basic Auth. I introduce some plugins like:

1: Basic Authentication handler
2: Application Passwords
3: WordPress REST API Authentication

Recommend use plugin: WordPress REST API Authentication

WordPress REST API Authentication

settings basic authentication

1: Create a new post

Step 1.1: create base64 credentials

base64_encode( 'USER:PASSWORD' )

Javascript:

btoa('USER:PASSWORD')

Result:

VVNFUjpQQVNTV09SRA==

Step 1.2: Create a new post

By CURL:

base64credentials="VVNFUjpQQVNTV09SRA=="
curl -sk --request POST --url https://tutorialspots.com/wp-json/wp/v2/posts --header "authorization: Basic $base64credentials" --header "content-type: application/json" -d '{"title":"demo post", "content":"demo post", "excerpt":"demo post", "date":"2020-06-01T02:35:31.198Z", "slug":"demo-post", "status":"draft" }'

See more fields: https://developer.wordpress.org/rest-api/reference/posts/#schema

PHP

$api_response = wp_remote_post( 'http://tutorialspots.com/wp-json/wp/v2/posts', array(
 	'headers' => array(
		'Authorization' => 'Basic ' . base64_encode( 'USER:PASSWORD' )
	),
	'body' => array(
	    'title'   => 'demo post',
		'status'  => 'draft',  
		'content' => 'demo post',
		'categories' => 1,  
		'tags' => '1,2,3',  
		'date' => '2020-06-01T02:35:31', 
		'excerpt' => 'demo post',		
		'slug' => 'demo-post' 
	)
) );
 
$body = json_decode( $api_response['body'] );
 
if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
	echo 'The post ' . $body->title->rendered . ' has been created successfully';
}

or PHP

function grabAPI($url,$method='POST',$data='')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);     
    curl_setopt($ch, CURLOPT_TIMEOUT,30);     
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $uaa = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'; 
    curl_setopt($ch, CURLOPT_USERAGENT, $uaa);
    $header = array(
        'authorization: Basic VVNFUjpQQVNTV09SRA==',
        'content-type: application/json',
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$method);
    if($data)
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    
    return curl_exec($ch);
}

$x = grabAPI('http://tutorialspots.com/wp-json/wp/v2/posts','POST','{"title":"demo post", "content":"demo post", "excerpt":"demo post", "date":"2020-06-01T02:35:31.198Z", "slug":"demo-post", "status":"draft" }');

2. Update POST

PHP

$api_response = wp_remote_post( 'http://tutorialspots.com/wp-json/wp/v2/posts/POSTID/', array(
 	'headers' => array(
		'Authorization' => 'Basic ' . base64_encode( 'USER:PASSWORD' )
	),
	'body' => array(
    	'title' => 'Demo post 2'
	)
) );
 
$body = json_decode( $api_response['body'] );
 
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
	echo 'The post ' . $body->title->rendered . ' has been updated successfully';
}

3. Delete a Post

Move to trash

If you want to delete permanently post, add ?force=true at the end of the request URI

PHP

$api_response = wp_remote_request( 'http://tutorialspots.com/wp-json/wp/v2/posts/POSTID', array(  
	'method'    => 'DELETE',
	'headers'   => array(
	    'Authorization' => 'Basic ' . base64_encode( 'USER:PASSWORD' )
	)
));
 
$body = json_decode( $api_response['body'] );
 
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
	if( $body->deleted == true ) {
		echo 'The post ' . $body->previous->title->rendered . ' has been completely deleted';
	} else {
		echo 'The post ' . $body->title->rendered . ' has been moved to trash';
	}
}

1 Comment

Leave a Reply