The MB Rest API extension is a powerful tool for developers that help you manage your custom fields and custom data via REST API in WordPress. It’s very helpful if you develop an application that uses WordPress as a back end such as a mobile app. Or you need to integrate WordPress with another system.
The extension supports retrieving and updating custom fields for posts, terms and users via REST API. Please note that it works only with custom fields created by the Meta Box plugin and extensions. For custom custom fields or fields created by other plugins, please refer to WordPress documentation.
For more information, please see the extension page.
Before installing the extension, you need to install Meta Box plugin first.
- Go to Plugins → Add New and search for Meta Box
- Click Install Now button to install the plugin
- After installing, click Activate Plugin to activate the plugin
The extension is just a WordPress plugin, you can install it like a normal WordPress plugin.
- Go to Plugins, click Add new, then click Upload plugin.
- Choose the
.zipfile you downloaded and click Install now
- After finishing upload, click Activate to finish.
Getting custom fields’ values in REST API responses
After installing and activating the plugin, create some meta box for your posts, terms or users. Please note that if you’re going to use term meta or user meta, you need to purchase MB Term Meta or MB User Meta accordingly.
Then just go to edit your posts, terms or users and add some values to your custom fields as you usually do.
Now you can retrieve these values when viewing REST API responses. The plugin automatically creates a new field
meta_box in the response JSON array, which holds all custom fields’ values.
If you create a meta box for posts like this:
And you get the post fields via REST API with URL
http://yourdomain.com/wp-json/wp/v2/posts/, you’ll see:
(The response is an JSON string. The screenshot above uses Firefox browser to beautify the result. You can see similar result in Google Chrome as well)
If you have multiple meta boxes, the plugin will merge all fields in those meta boxes and returns a single array in the
meta_box field of the response.
Please note that the similar result will be returned for single post, terms, single term, users, single user. E.g. it works with the following endpoint in REST API:
http://yourdomain.com/wp-json/wp/v2/posts/ http://yourdomain.com/wp-json/wp/v2/posts/1234 http://yourdomain.com/wp-json/wp/v2/categories/ http://yourdomain.com/wp-json/wp/v2/categories/1234 http://yourdomain.com/wp-json/wp/v2/users/ http://yourdomain.com/wp-json/wp/v2/users/1234
Updating custom fields value via REST API
To update custom fields value via REST API, simply add a new field
meta_box to your POST requests. This field should be either an array of
'field_id' => 'field_value' or a JSON string of an this array.
The plugin will automatically fetch the fields and update the corresponding custom fields.