• WordPress

Understanding the WordPress REST API: A guide for developers

  • Felix Rose-Collins
  • 4 min read
Understanding the WordPress REST API: A guide for developers

Intro

Creating a website is a complex and multistep process. To start, you first need to find a quality hosting solution for that. If you a going to create a new website and don’t have a very high demand for resources and very high traffic expected, a VPS server is a versatile solution that will be suitable for most websites. If the payment method matters to you, you should know that you can choose among a variety of options, such as credit card VPS, bitcoin VPS, AliPay VPS, etc.

Another fundamental aspect of creating a website is the choice of a CMS – content management system, a special tool that is used to fill the website with content and modify it. If your choice is WordPress, your developers will have to deal with WordPress REST API.

Wordpress is one of the most popular CMS for building a website nowadays. REST API in turn is an essential interface that is used for working with a Wordpress website. Due to REST API, your applications get the possibility to interact with your website through JSON files. In this way, developers can build custom applications, websites, and plugins on top of Wordpress. REST API allows developers to interact with Wordpress data by means of HTTP commands.

In the following, we are going to have a look at some essential commands that will be useful for any developer.

WordPress REST API: Essentials

WordPress REST API: Essentials

GET:

It is used to retrieve data from the WordPress database using HTTP GET method. This command is one of the most common ones, being the first command you use before actually doing something with your data.

It allows the retrieval of a specific resource such as a post, page, or custom post type without modifying it.

To use the method, you need to send a request to a specific endpoint with a given set of parameters. The endpoint is presented by an URL of the resource with the parameters you want to retrieve – for example, the ID of the resource, filtering options, pagination information, and other query parameters.

Meet Ranktracker

The All-in-One Platform for Effective SEO

Behind every successful business is a strong SEO campaign. But with countless optimization tools and techniques out there to choose from, it can be hard to know where to start. Well, fear no more, cause I've got just the thing to help. Presenting the Ranktracker all-in-one platform for effective SEO

We have finally opened registration to Ranktracker absolutely free!

Create a free account

Or Sign in using your credentials

If you type this:

GET http://yoursite.com/wp-json/wp/v2/posts/983

you’ll get the details of the post with this particular ID such as its title, content, author, and other metadata.

Furthermore, you can get the information on a list of resources. For example, if you want to see all published ones, you should add /wp/v2/posts?status=publish to the endpoint request:

GET http://yoursite.com/wp-json/wp/v2/posts?status=publish

POST:

This command is used to add something new to your website. This can actually be a post, but it can also be used for pages, comments, and other attachments.

To create a new post, add /wp/v2/posts to the endpoint.

POST http://yoursite.com/wp-json/wp/v2/posts

Then, in the payload, you’ll be able to include the data for the new post, such as its title, content, author, and other metadata:

"title": "My New Post",
"content": "This is the content of my new post.",
"author": 1,
"status": "publish"

PUT:

POST is used to create empty drafts for pages and posts. To further update one with new content, you can use the PUT command. This can be further used to publish previously created drafts.

To see all the current drafts, you can use the POST command this way:

POST http://yoursite.com/wp-json/wp/v2/posts/?status="draft".

After this, you can use the PUT command to modify the status of a particular draft by entering its ID:

PUT http://yoursite.com/wp-json/wp/v2/posts/983

This will access your post with an option to modify it. Now you have to switch its status from “draft” to “publish”:

"status" = "publish"

Additionally, you can add more content to it using “status” argument:

"status" = "publish"
"content" = "content here"

DELETE:

Another essential command used for deleting posts, pages, content, and users. A deleted post will be put in the trash first.

To delete a particular post, for example, the one created above, enter something like this:

DELETE  http://yoursite.com/wp-json/wp/v2/posts/983

If you want to delete the post for good without sending it to the trash bin, add ?force=true after it:

DELETE http://yoursite.com/wp-json/wp/v2/posts/983?force=true

OPTIONS:

If you want to check a resource for the communication options that are available for it, you can use this command. When you enter the command, in the output, you’ll get the list of communication methods that are available for a particular resource together with some other relevant information, like supported authentication schemes, request headers, and content types.

This command allows retrieval of the headers of the content without having to do with actual content. In this way, a developer can get the necessary information about pieces of content, such as caching information, content type, and content length, without the need to download them.

PATCH:

This command allows you to update a particular part of a piece of content. It’s somewhat similar to PUT, but you don’t have to fully replace a particular resource. This way you can update the title or the content of a post, as well as metadata that is associated with a user, while the rest remains the same.

When using the command, you have to specify, which changes you want to perform. For example, to edit the title, you’ll have to add this to the request body:

"title": "New Post Title"

Conclusion

WordPress REST API is a multifunctional interface that allows your applications to interact with your WordPress site. The interface is managed with the help of various commands, to which we have dedicated today’s article. We hope that this information was useful, thank you for your time!

Felix Rose-Collins

Felix Rose-Collins

Ranktracker's CEO/CMO & Co-founder

Felix Rose-Collins is the Co-founder and CEO/CMO of Ranktracker. With over 15 years of SEO experience, he has single-handedly scaled the Ranktracker site to over 500,000 monthly visits, with 390,000 of these stemming from organic searches each month.

Start using Ranktracker… For free!

Find out what’s holding your website back from ranking.

Create a free account

Or Sign in using your credentials

Different views of Ranktracker app