WordPress Rest api Example and Uses

WordPress is popular PHP CMS for blogs, forums or websites. Rest API is a popular way to communicate different applications on the web in a gentle way. The Rest API is unable to access your site’s data through an easy-to-use HTTP REST API.

By default, WordPress does not provide any Restful API to communicate database data, We need to install WordPress REST API (Version 2) plugin that provides an easy-to-use REST API, available via HTTP. You can get the site’s data in simple JSON format, including users, posts, taxonomies and more. You can also fetch or update data using HTTP request API.

WordPress API Common HTTP Methods

  1. POST: Create a new post
  2. GET: fetch record from WordPress database
  3. PUT: This HTTP method is used to update record
  4. DELETE: Use to delete a record from the WordPress database

You can use any HTTP GET, PUT, POST and DELETE type request for any crud operations like get, update and delete a record. You can simply access all post information using wp-json/wp/v2/posts Rest URL. You can access the following types of objects of Information using WordPress Rest Api v2.0.

  1. Posts
  2. Pages
  3. Media
  4. Post meta
  5. Post revisions
  6. Comments
  7. Taxonomies
  8. Terms
  9. Users

You can get all WordPress rest end points from www.wordpresurl.com/wp/v2/.

Simple Posts Object Rest API

#RouteMethodTypeFull routeDescription
1/postsGETJSONwordpressURL.com/wp-json/wp/v2/posts/Get all posts data
2/posts/{id}GETJSONwordpressURL.com/wp-json/wp/v2/posts/{id}Get a single post data
3/posts/{id}POSTJSONwordpressURL.com/wp-json/wp/v2/posts/{id}Update post data
4/posts/{id}DELETEJSONwordpressURL.com/wp-json/wp/v2/posts/{id}Delete post from wordpress database

Simple Taxonomies Object Rest API

#RouteMethodTypeFull routeDescription
1/taxonomiesGETJSON/wp-json/wp/v2/taxonomiesGet all taxonomies data
2/taxonomies/{id}GETJSON/wp-json/wp/v2/taxonomies/{id}Get a single taxonomy data
3/taxonomies/{id}POSTJSON/wp-json/wp/v2/taxonomies/{id}Update taxonomy data
4/taxonomies/{id}DELETEJSON/wp-json/wp/v2/taxonomies/{id}Delete taxonomy from wordpress database

Simple Categories Object Rest API

#RouteMethodTypeFull routeDescription
1/taxonomiesGETJSON/wp-json/wp/v2/categoriesGet all categories data
2/categories/{id}GETJSON/wp-json/wp/v2/categories/{id}Get a single category data
3/categories/{id}POSTJSON/wp-json/wp/v2/categories/{id}Update category data
4/categories/{id}DELETEJSON/wp-json/wp/v2/categories/{id}Delete category from WordPress database

WordPress Page Rest API

#RouteMethodTypeFull routeDescription
1/pagesGETJSON/wp-json/wp/v2/pagesGet all pages data
2/pages/{id}GETJSON/wp-json/wp/v2/pages/{id}Get a single page data
3/pages/{id}POSTJSON/wp-json/wp/v2/pages/{id}Update page data
4/pages/{id}DELETEJSON/wp-json/wp/v2/pages/{id}Delete page from wordpress database

Simple Example of WordPress Api 2.0 with Guzzle PHP Client

Here, I am accessing WordPress 2.0 restful API using the Guzzle PHP client. You can use any other programming language framework like spring boot, django, ruby etc as well, The whole idea is how to pass parameters with Rest Request using WordPress API and Rest Client.

Before accessing any WP API rest service, you must have an authentication basic token of WordPress API. You can use the Basic-Auth WordPress plugin. The Basic-Auth provides basic authentication access to WordPress API.

I also shared tutorials of Simple Example Guzzle Rest Client With Rest API.

In this example, I am creating a new post into the WordPress database using WordPress API. I am creating a Guzzle client and passing base64 username:password string within request header.

 'http://www.yourblogname.com/wp-json/wp/v2/',
    // You can set any number of default request options.
    'timeout'  => 2.0,
	'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", 'Authorization' => "Basic " . $base64],
    //ssl false
    'verify' => false
]); 

Where is :

  • $base64: This variable will contains base64 string of username and password
  • base_uri: The WordPress host API URL path
  • headers: This will contain request header parameters
  • timeout: Request timeout in sec.
  • verify : SSL verification false

Now, I will create post parameters and passed to the guzzle client. We finally send a request to WordPress API.

$params = array(
	"title" => "Hello Updated World!",
	"content_raw" => "Howdy updated content.",
	"date" => "2017-02-01T14:00:00+10:00"
	);
//print_r(json_encode($params));die;
//$response = $client->request('POST');
$response = $client->post('posts/',
    [ 'body' => json_encode($params)]
);
echo "";
echo $response->getBody();

As you can see, I created Post data and stored into $params variable. I use the Post method($client->post()) of Guzzle client and passed data into body of request.

When a request is successfully completed, you will receive a JSON object as a response informing you that a new WordPress post has been created and saved as a draught (default WordPress post status).