Starting with Ultimate Fields 2, the plugin can automatically add data to the WordPress REST API.
Ultimate Fields can associate various locations with endpoints in the API. For the built-in content types (all except Options), the fields are added to the normal endpoints in WordPress. For the options page, there is a separate endpoint, which exposes all registered options.
||Created by Ultimate Fields
||Same as WP Post Types (ex.
||Same as WP Taxonomies (ex.
The tab contains a couple of fields:
- Expose allows you to associate the container with the REST API.
- When exposed, there will be a second field called API FIelds. This is a repeater, which lets you choose which fields to expose and whether you would like them to be read-only or not.
Writeable fields will follow the same access logic as WordPress' built-in fields. If a user can edit them, they will be also able to edit the custom fields, which you created.
For Options Pages, the user must be logged in and have the same capability as the options page, which containers are added to.
Usage in PHP
Adding fields to the rest API is done through the
api_fields arguments in the
$args array or the
expose_api_fields method of a location.
api_fields argument and the
expose_api_fields method accept an array of fields. The array can contain either simply the
name of the field or a pair of the name of the field and the access type:
If you include just the field name, it will be added as a read-only field. To make a field editable, you need to use the
WP_REST_Server::EDITABLE constant as the value.
If you are using the
->add_location method, you can do it this way: