Comments (2)
I've added this section to the Modifying Responses guide in the handbook: I'm going to mark this as resolved with this addition, we can re-open if further clarification is needed.
Using register_rest_field
vs register_meta
There are two methods which can be used to add data to WordPress REST API responses, register_rest_field
and register_meta
.
register_rest_field
may be used to add arbitrary fields to any REST API response, and can be used to both read and write data using the API. To register a new REST field you must provide your own callback functions to get or set the field's value, as well as manually specify your own schema definition for the field.
register_meta
is used to whitelist an existing custom meta value for access through the REST API. By setting a meta field's show_in_rest
parameter to true
, that field's value will be exposed on a .meta
key in the endpoint response, and WordPress will handle setting up the callbacks for reading and writing to that meta key. This is much simpler than register_rest_field
, with one caveat:
[alert]
Meta fields set toshow_in_rest
usingregister_meta
are registered for all objects of a given type. If one custom post type shows a meta field, all custom post types will show that meta field.
[/alert]
This is because meta registration is currently handled at the base level of WordPress object (post, user, etc) and cannot be specified per "subtype" (e.g. custom post types). This limitation may be improved in future versions of WordPress, but if you are trying to add a custom field to a specific API endpoint's response then register_rest_field
is currently recommended over register_meta
.
from docs-v2.
Also I think that should be explained if is wrong or right to use register_meta() instead of register_rest_field() when the meta field is intended to be used only with one or some custom posts (or taxonomies).
from docs-v2.
Related Issues (20)
- Broken Markdown in the filter reference. HOT 1
- Can i Make a Chinese Copy? HOT 2
- Document Disabling Endpoints HOT 1
- What's Changed: v2 Beta 1 page HOT 1
- BBPress Endpoint HOT 3
- XML Response HOT 1
- Add page to reference to handle the new settings and options endpoints
- Add mention of JWT plugin to authentication docs
- Create User Guides section on developer.wordpress.org HOT 3
- Client JS Documentation. HOT 1
- Create necessary theme files / plugins to handle auto syncing on .org
- Merge classes articles
- Create user guide for how to query collections
- Add a user guide for querying the API from the browser
- Consider shortening parent page URLs in developer handbook HOT 2
- Clarify HAL comment in rest handbook reference index HOT 4
- Please add mention of this plugin: "JAVASCRIPT CLIENT HANDLE for the WP REST-API" in the Compatible Plugins list
- Posting on a template
- All generated HTMLs are just redirects HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docs-v2.