The following is a snapshot of some of the changes to the REST API The REST API is an acronym for the RESTful Application Program Interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. It is how the front end of an application (think โphone appโ or โwebsiteโ) can communicate with the data store (think โdatabaseโ or โfile systemโ) https://developer.wordpress.org/rest-api/. in WordPress 5.7. For more details, see the full list ofย closed tickets.
Endpoint Changes
Posts Collection Tax Query Accepts include_children
Introduced inย 50157, the REST API posts collection endpoints have been updated to allow a more complex syntax for specifying theย tax_queryย used when querying posts. Each taxonomy A taxonomy is a way to group things together. In WordPress, some common taxonomies are category, link, tag, or post format. https://codex.wordpress.org/Taxonomies#Default_Taxonomies.โs query parameters can now both accept a list of term ids or an object with aย termsย property.ย
Hierarchical taxonomies support anย include_childrenย property alongsideย terms. By default itโs disabled, but if set toย trueย the flag is enabled for the generatedย tax_queryย to enable searching for posts that have the given terms or terms that are children of the given terms.
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '@wordpress/api-fetch';
const query = {
categories: {
terms: [ 3, 5, 7 ],
include_children: true,
},
};
apiFetch( { path: addQueryArgs( '/wp/v2/posts', query ) } );
See #39494 for more details.
Supportย modified_beforeย andย modified_afterย Query Parameters
Introduced inย 50024ย the REST API posts collection endpoints now acceptย modified_beforeย andย modified_afterย query parameters to query posts based on the post modified date instead of the post published date.
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '@wordpress/api-fetch';
const query = {
modified_after: '2020-12-01T00:00:00Z',
modified_before: '2020-12-31T23:59:59Z',
};
apiFetch( { path: addQueryArgs( '/wp/v2/posts', query ) } );
As a result of this change, the posts controller now uses aย date_queryย with a separate clause for each date related query parameter instead of a single clause with theย beforeย andย afterย flags set.
See #50617 for more details.
Multiple Meta Meta is a term that refers to the inside workings of a group. For us, this is the team that works on internal WordPress sites like WordCamp Central and Make WordPress. Values for a Key Can Be Deleted by Sending an Empty Array
Previously, to remove all values for a specific meta key, passingย nullย as the value for that meta key was required. Afterย 49966ย an empty array can be passed instead.
register_post_meta( 'post', 'my_meta_key', array(
'type' => 'string',
'show_in_rest' => true,
) );
import apiFetch from '@wordpress/api-fetch';
const data = {
meta: {
my_meta_key: [],
}
};
apiFetch( { data, method: 'PUT', path: '/wp/v2/posts/1' } );
See #50790 for more details.
All Themes are Returned via the Themes Controller
As ofย 49925, the themes endpoint now returns all themes installed on the site, not just the active theme. By default, the endpoint returns bothย activeย andย inactiveย themes, but theย statusย query parameter can be used to limit the list to themes with the desired status.
Note, theย theme_supportsย value is only exposed for theย activeย theme. The field is omitted forย inactiveย themes since declaring the supported theme features requires calls toย add_theme_supportย which can only be done if the theme is active.
When querying solely forย activeย themes, the only permission required is to be able to edit posts of anyย show_in_restย post type. However when querying forย inactiveย themes theย switch_themesย orย manage_network_themesย capability is required.
In addition to the changes to the collection endpoint, a new single theme endpoint is available. For example,ย /wp/v2/themes/twentytwentyone will return information about Twenty Twenty One. For convenience, a link is also added to the currently active theme in the REST API Index if the current user has the requisite permissions. This is the recommended way for applications to discover the currently active theme.
{
"name": "WordPress Develop",
"_links": {
"help": [
{
"href": "http://v2.wp-api.org/"
}
],
"wp:active-theme": [
{
"href": "https://wordpress.test/wp-json/wp/v2/themes/twentytwentyone"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}
Lastly, theย page,ย per_page,ย searchย andย contextย query parameters are no longer exposed in the index as they are not supported by this endpoint.
See #50152 for more details.
Image Editor Accepts a List of Modifiers
Theย /wp/v2/media/<id>/editย endpointย introduced in WordPress 5.5 came with a limited API An API or Application Programming Interface is a software intermediary that allows programs to interact with each other and share data in limited, clearly defined ways. that accepted top-level rotation and crop declarations. Inย 50124ย this API was made more powerful and flexible by accepting an array of modifications in the newย modifiersย request parameter.
import apiFetch from '@wordpress/api-fetch';
const data = {
modifiers: [
{
type: 'crop',
args: {
left : 0,
top : 0,
width : 80,
height: 80
}
},
{
type: 'rotate',
args: {
angle: 90
}
}
]
};
apiFetch( { data, method: 'POST', path: '/wp/v2/media/5/edit' } );
The previous query parameters have been marked as deprecated, but will continue to function as normal and do not currently issue deprecation warnings. Clients are encouraged to switch to the new syntax.
To alleviate server resources, whenever possible, clients should simplify redundant modifications before sending the request.
See #52192 for more details.
Parameter Validation
Non-String Enums
Inย 50010ย support for type coercion was added to theย enumย JSON JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML. Schema keyword. Previously, theย enumย keyword was validated by perform a strict equality check. Forย stringย types this is generally ok, but it prevented using alternative types likeย numberย when rich type support isnโt available.
Now the same level of type coercion/sanitization is applied when validating enum as all other validation checks. This means that a value ofย "1"ย will be accepted for an enum of [ 0, 1 ]. Additionally,ย objectย types now properly ignore key order when checking for equality.
See #51911 for more details.
Validation Error Codes
As ofย 50007, theย rest_validate_value_from_schemaย function now returns specific error codes for each validation failure instead of the genericย rest_invalid_param. For instance, if more array items are given than allowed byย maxItems, theย rest_too_many_itemserror code will be returned.
See #52317 for more details.
Return Detailed Error Information from Request Validation
Previously when a parameter failed validation only the first error message specified in theย WP_Errorย instance was returned to the user. Sinceย 50150 the REST API will now return detailed error information as part of theย detailsย error data key.
{
"code": "rest_invalid_param",
"message": "Invalid parameter(s): enum_a, enum_b",
"data": {
"status": 400,
"params": {
"enum_a": "enum_a is not one of a, b, c.",
"enum_b": "enum_b is not one of d, e, f."
},
"details": {
"enum_a": {
"code": "rest_not_in_enum",
"message": "enum_a is not one of a, b, c.",
"data": {
"enum": ["a", "b", "c"]
}
},
"enum_b": {
"code": "rest_not_in_enum",
"message": "enum_b is not one of d, e, f.",
"data": {
"enum": ["d", "e", "f"]
}
}
}
}
}
See #50617 for more details.
Application Passwords
Fine Grained Capabilities
When the Application Passwords REST API controllers were introduced, theย edit_userย meta capability was used for all permission checks. As ofย 50114, the REST API now uses specific meta capabilities for each action type.
create_app_passwordlist_app_passwordsread_app_passwordedit_app_passworddelete_app_passworddelete_app_passwords
By default, these capabilities all map toย edit_userย however they can now be customized by using theย map_meta_capย filter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output..
See #51703 for more details.
Introspection Endpoint
50065ย introduces a new Application Passwords endpoint for introspecting the app password being currently used for authentication. This endpoint is accessible viaย /wp/v2/users/me/application-passwords/introspectย and will return the same information as the other endpoints. This allows for an application to disambiguate between multiple installations of their application which would all share the sameย app_id.
Clients can use this information to provide UI User interface hints about how the user is authenticated, for instance by displaying the App Passwordsโs label. Or when their application is uninstalled by the user, they can automatically clean up after themselves by deleting their App Password.
See #52275 for more details.
Unique Application Names
As ofย 50030ย the Application Passwords API enforces that each App Password has a unique name that cannot consist solely of whitespace characters. Additionally, invalid A resolution on the bug tracker (and generally common in software development, sometimes also notabug) that indicates the ticket is not a bug, is a support request, or is generally invalid. characters are stripped from the provided application name.
See #51941 for more details.
Props to @flixos90, @desrosj for proofreading.
#5-7, #dev-notes, #rest-api
You must be logged in to post a comment.