Deleting A WordPress Blog Post Using The WordPress REST API

The NextJs application allows users to delete blog posts using the WordPress REST API by sending HTTP DELETE requests to the endpoint specified by the WordPress Developers website.

Users are required to login before the delete button appears for a blog post. Upon clicking the delete button the user is provided visual feedback that the post has been deleted in the form of a red background for the post listing.

The following screenshot shows the network tab found within the Chrome developer tools. The highlighted network request is for the deletion of a blog post with ID 168. The documentation for sending CRUD operations to a WordPress servers REST API can be found on the WordPress Developer website (WordPress, n.d.).

The following code extract provides detail on how the request is sent, using the fetchJSON function. Much of the contents of this functional component have been omitted due to its length, with the most important part of the mechanism being shown below.

Deleting posts using WordPress can be achieved by sending an HTTP DELETE request to the https://example.com/wp-json/wp/v2/posts/<id> route where the <id> slug is equal to the ID of the post to be deleted.

References

WordPress (n.d.). Posts | REST API Handbook. [online] WordPress Developer Resources. Available at: https://developer.wordpress.org/rest-api/reference/posts/#delete-a-post [Accessed 1 May 2021].

Comments

Popular posts from this blog

WordPress Modifying A Child Theme & Using The OpenWeatherMap API

Creating A WordPress Post Using The REST API

Consuming The WordPress Posts Via The REST API