MB Rest API

Improve this page

Installation

Before installing the extension, you need to install Meta Box plugin first.

  1. Go to Plugins → Add New and search for Meta Box
  2. Click Install Now button to install the plugin
  3. 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.

  1. Go to Plugins, click Add new, then click Upload plugin.
  2. Choose the .zip file you downloaded and click Install now
  3. 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.

For example:

If you create a meta box for posts like this:

meta box for posts

And you get the post fields via REST API with URL http://yourdomain.com/wp-json/wp/v2/posts/, you’ll see:

custom fields values in rest api response

(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.