WordPress is the most popular CMS used on the internet and has been for some time. Because of popularity and being open source software there are 100s/1000s of people looking to improve the functionality of WordPress. One of the most exciting new features of WordPress is the HTTP REST API development.
/wp-json/wp/v2/posts or if you want to search for posts with a certain keyword then you can make a GET request to
The way your RESP API will be able to be accessed by external websites is by authenticating the HTTP request by using either cookie authentication or OAuth authentication. The REST API recommends that authentication should come down to the type of request you're making. - Are you a plugin/theme running on the site? Use cookie authentication
Cookie authentication is already built into WordPress it's used when you log into your WordPress dashboard it will store a cookie on your machine to authenticate your requests, the REST API does this by WordPress Nonces. OAuth Authentication is the main method when working with external websites. This process gives clients access to the API via tokens. Tokens can have an expiry to even turned off to stop clients accessing the API anymore. To use OAuth with the WordPress REST API you will also need to install the plugin OAuth. WP API OAuth When this is installed you'll need to create a new client which can be created from the admin area. The client will need be assigned a token allowing it access to the REST API by sending the authentication in the HTTP headers of the request.
As you need a plugin to access the REST API in WordPress one of the main worries is how much coverage do you get to the data in your WordPress CMS. Let's have a look at what data you can access from the REST API. To see the data you can access you can view the API document using the link below: WordPress REST API Reference
Using the API you can access all the posts stored in your WordPress CMS.
To get a list of posts where you will commonly see on the homepage of a WordPress theme, where it displays the latest 10 posts of your blog you can use the following GET request.
GET /wp/v2/posts The following arguments can be passed to the GET with through querystring to change the criteria of the posts returned. An example of using these arguments for say getting all posts by a certain author can be done by using the
author argument with the ID of the author.
GET /wp/v2/posts?author=1 If you want to create a search query then you can use the
search passing in the search term that you want to use.
You're able to get a single post by the post ID by querying the API by using the following GET request.
GET /wp/v2/posts/:id This will return the post information for this post ID. But in most websites you'll want to use a nicer looking permalink with the post slug, then you'll be able to get the full post information by using the GET request.
DELETE /wp/v2/posts/<id></id> Post API Reference
When a post is being edited WordPress admin area will auto-save so the post, this adds a revision record into the database, the same happens when you update a post. This revision record can be used to display the history of the content. The following endpoints can be used to interact with the post revision records.
DELETE /wp/v2/posts/:parent_id/revisions/:id Post Revision API Reference
Pages is just another post type in the cMS, the endpoints therefore will be similar to the posts endpoints but change the posts with pages.
POST /wp/v2/pages/:id Pages API Reference
The media endpoints are used to handle the request for any sort of media files attached to a post such as featured images. A media endpoint can be used to customise the data attached to a certain image, such as the stored title, alt tag etc.
DELETE /wp/v2/media/:id Media API Reference
If you have multiple post types in your application then you can use the following endpoints to interact with post type data.
GET /wp/v2/types/:type Post Types API Reference
Post statuses are used in the author workflow to show what stage the post is in, you can use these endpoints to get the registered post statuses in your applications.
GET /wp/v2/statuses/:status Post Statuses API Reference
Using the following end points you can interact with comments published on the blog. This allows you to get all comments for a certain post and even create comments on your theme.
DELETE /wp/v2/comments/:id Comments API Reference
Most websites will use taxonomies to organise the content structure of the site, using the following endpoints allow you to get a list of the taxonomies used on your website.
GET /wp/v2/taxonomies/:taxonomyId Taxonomies API Reference
Use the following endpoints to interact with the category taxonomy on your WordPress website.
DELETE /wp/v2/categories/:id Categories API Reference
Use the following endpoints to interact with the tags taxonomy on your WordPress website.
DELETE /wp/v2/tags/:id Tags API Reference
If you need to interact with Users on your WordPress website then there are the following endpoints to get users or create new users.
DELETE /wp/v2/users/:id Users API Reference
In the next couple of weeks we're going to investigate how you can create a WordPress theme using VueJS to access the REST API to replace the PHP version of themes. I'm also going to see if I can replace the existing theme on Paulund and use a VueJS theme moving forward.