How To Use WordPress REST API – Create, Update Or Delete Posts Using Basic Auth

Here we learn about how to use WordPress REST API – create, update or delete posts using basic auth.

First, you have to download a special plugin. below the screenshot:

Also, You have to install the “Application Passwords” plugin, it is available from the WordPress plugins repository.

That your REST API is not turned off, then generate an app password there.

Create a Post :

You can see wp_remote_post() function more details for this function click here.

The WordPress function wp_remote_post(), so you can insert in somewhere inside WP environment.

<?php
  $api_response = wp_remote_post( 'https://YOUR_DOMAIN_NAME/wp-json/wp/v2/posts', array(
    'headers' => array(
      'Authorization' => 'Basic ' . base64_encode( 'LOGIN:PASSWORD' )
    ),
    'body' => array(
      'title'   => 'Test Post Title',
      'status'  => 'draft', // ok, we do not want to publish it immediately
      'content' => 'Test Content Here...',
      'categories' => 5, // category ID
      'tags' => '1,4,23' // string, comma separated
      'date' => '2020-01-01T10:00:00', // YYYY-MM-DDTHH:MM:SS
      'excerpt' => 'Test Extra Post Text Here...',
      'password' => '111@#111',
      'slug' => 'test-post-title' // part of the URL usually
      // more body params are here:			
    )
  ) );
 
  $body = json_decode( $api_response['body'] );
 
  // you can always print_r to look what is inside
  // print_r( $body );
 
  if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
    echo 'The post ' . $body->title->rendered . ' created successfully';
  }
?>

 

Update a Post :

Your post title update using {POST_ID}.

<?php
$api_response = wp_remote_post( 'https://WEBSITE/YOUR_DOMAIN_NAME/v2/posts/{POST_ID}/', array(
  'headers' => array(
    'Authorization' => 'Basic ' . base64_encode( 'LOGIN:PASSWORD' )
  ),
  'body' => array(
    'title' => 'My test post update'
  )
) );
 
$body = json_decode( $api_response['body'] );

if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
  echo 'The post ' . $body->title->rendered . ' updated successfully';
}
?>

 

Delete a Post :

https://WEBSITE/wp-json/wp/v2/posts/{POST_ID}?force=true

If you used the above URL end of URL add “?force=true” post is permanently removed.

<?php
$api_response = wp_remote_request( 'https://YOUR_DOMAIN_NAME/wp-json/wp/v2/posts/{POST_ID}', array(  // ?force=true to skip trash
  'method'    => 'DELETE',
  'headers'   => array(
    'Authorization' => 'Basic ' . base64_encode( 'LOGIN: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 . ' completely deleted';
  } else {
    echo 'The post ' . $body->title->rendered . ' moved to trash';
  }
}
?>	

1 Comment

  1. praveen

    Hi Rahu,
    Thanks for the aboce Code. Is it possible to Update Orders Meta value. If is can you please let me know how?
    Thanks

    0
    0
    Reply

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories