Introduction to WordPress REST API

Introduction to WordPress REST API

Out of the box, WordPress comes with all the necessary functionality to work with APIs. Interaction with the WordPress REST API is handled via endpoint classes located in the “/wp-includes/rest-api/endpoints” directory. So, if you have any questions about how something works, what filters are supported in search, or which hooks are available — I recommend first taking a look at that directory. It contains plenty of information to help you understand, sometimes confusingly presented, but thanks to modern editors, everything is searchable.

By default, WordPress only allows reading data via the API in JSON format. That is, you won’t be able to create, edit, or delete data. For those actions, you will either need to install third-party plugins (which we will do), or develop the necessary functionality yourself (which we will also do).

Keep in mind that using third-party plugins, especially those with low ratings or outdated versions in the WordPress repository, may introduce vulnerabilities or bugs.

What is the URL for accessing the WordPress REST API?

All WP endpoints are available at the link — “//your-site-address/wp-json/wp/v2”. There may also be endpoints from other applications at “//your-site-address/wp-json/something-else”. You can view all available API information at “//your-site-address/wp-json/”. Pay special attention to the “routes” section — it lists all available routes.

How to filter data via API?

Default WordPress endpoints — such as posts, pages, categories, etc. — already support predefined filtering parameters. If we take posts, they are available at “//your-site-address/wp-json/routes/wp/v2/posts”. Then look for the “endpoints” section (nested, but not reflected in the URL). It contains two elements — GET and POST. The first is for reading, the second is for creating entries.

As mentioned earlier, POST won’t work out of the box. To create records via the JSON API, the user must first be authenticated. The GET section lists the available search arguments and their expected data types — which is important, otherwise WordPress will return warnings.

How to create a custom endpoint in WordPress?

For custom post types (such as post or page), you define this when registering the post type:

register_post_type(‘my_post_type’, array(
	'show_in_rest'	=> true, //or FALSE
));

 

You can also create your own custom endpoint for specific tasks (e.g., updating WooCommerce prices by SKU). To do that, use the following hook:

add_action('rest_api_init', function()
{
	register_rest_route('project/v1', 'update', [
		'methods' => WP_REST_Server::EDITABLE,
		'callback' => function(WP_REST_Request $Request)
		{

		},
		'args' = > [

		]
	]);
});

Overall, the topic of the WordPress API (with JSON) is quite interesting. In this article series, I will try to cover it as thoroughly as possible.

Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *