How to Delete a Post Using the WordPress REST API | Full Code

WordPress REST API Delete Post Tutorial: Deleting a WordPress post programmatically can be achieved using the WordPress REST API. In this tutorial, we will guide you through a step-by-step process using PHP to delete a post on your WordPress site. We’ll cover authentication, constructing the API endpoint, and handling the deletion request.

Prerequisites:

  • A WordPress site with admin privileges
  • A user ID with the required permissions
  • An application password for secure authentication

Generate WordPress Application Password Here

WordPress REST API Delete Post Tutorial Steps

Total Time: 5 minutes

Step 1: Set Up Authentication

Step 2: Specify the Post to Delete

Step 3: Construct the API Endpoint

Step 4: Make the DELETE Request

Step 5: Process the Response

Step 1: Set Up Authentication

Replace the placeholders in the code with your WordPress site URL, user ID, user password, and application password. The authentication credentials are essential for making secure requests to the REST API.

$site_url = 'https://your-wordpress-site.com';
$user_id = "your_user_id";
$user_app_password = "your_wordpress_application_password";

$credentials = $user_id . ':' . $user_app_password;
$token = base64_encode($credentials);
$header = array('Authorization: Basic ' . $token);

$user_app_password = "your_wordpress_application_password"; is required for secure authentication when interacting with the WordPress REST API. WordPress introduced application passwords as a more secure way to handle authentication for external applications or scripts.

The WordPress REST API requires authentication to ensure that only authorized users or applications can perform certain actions, such as creating, updating, or deleting posts. The application password serves as the authentication token for these API requests.

If you do not know how to generate the wordpress application password then follow this guide to create one for your wordpress website.

Step 2: Specify the Post to Delete

Identify the post you want to delete by setting the $post_id variable to the corresponding post ID.

$post_id = 123; // Replace with the actual post ID

Also learn how to Create a Post with WordPress REST API with Example & Full Code

Step 3: Construct the API Endpoint:

Build the URL for the REST API endpoint, including the post ID.

$delete_url = $site_url . '/wp-json/wp/v2/posts/' . $post_id;

Optionally, if you want to bypass the trash and force deletion immediately, you can append ?force=true to the endpoint:

$delete_url = $site_url . '/wp-json/wp/v2/posts/' . $post_id . '?force=true';

Step 4: Make the DELETE Request:

Create a cURL request to the constructed endpoint, specifying the DELETE method and including the authentication header.

$ch = curl_init($delete_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

Step 5: Process the Response:

Check for errors and decode the response. Print the API call result data for debugging purposes.

if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    $result = json_decode($response, true);

    // Handle the response based on the result
    // ...

    // Close the cURL session
    curl_close($ch);
}

Complete Code: WordPress REST API Delete Post

<?php
    // Set your WordPress site URL and authentication credentials
    $site_url = 'https://example.com';  // Your wordpress site url
    $user_id = "";  // WordPress User ID with admin privileges
    $user_password = "";    // WordPress User Password
    $user_app_password = "";   // WordPress Application Password
    
    $credentials = $user_id . ':' . $user_app_password;
    $token = base64_encode($credentials);
    $header = array('Authorization: Basic ' . $token);

    // Set the post ID you want to delete
    $post_id = 123; // Replace with the actual post ID

    // Create the URL for the REST API endpoint
    $delete_url = $site_url . '/wp-json/wp/v2/posts/' . $post_id;
    
    //Bypass trash
    $delete_url = $site_url . '/wp-json/wp/v2/posts/' . $post_id . '?force=true';

    // Create the cURL request
    $ch = curl_init($delete_url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute the cURL request
    $response = curl_exec($ch);

    // Check for errors
    if (curl_errno($ch)) {
        echo 'Error: ' . curl_error($ch);
    } else {
        // Decode the response
        $result = json_decode($response, true);

        // Printing the API Call result data in array format
        echo "<pre>";
        print_r(json_decode($response, true));
        echo "</pre>";
        if(isset($result['deleted']) && $result['deleted'] == 1) {
            echo "Post has been deleted successfully.";
        }
        elseif(isset($result['status']) && $result['status'] == "trash") {
            echo "Post has been deleted successfully.";
        }
        else {
            echo "Error deleting post";
        }
    }

    // Close the cURL session
    curl_close($ch);

?>

Conclusion

By following these steps, you can create a PHP script to programmatically delete WordPress posts using the REST API. Customize the script to suit your needs, and remember to handle errors gracefully to ensure a smooth user experience.

Read the WordPress REST API official documentation for more information.

Leave a Comment