* Version number for our API.
define( 'REST_API_VERSION', '2.0' );
* Registers a REST API route.
* Note: Do not use before the {@see 'rest_api_init'} hook.
* @since 5.1.0 Added a `_doing_it_wrong()` notice when not called on or after the `rest_api_init` hook.
* @since 5.5.0 Added a `_doing_it_wrong()` notice when the required `permission_callback` argument is not set.
* @param string $route_namespace The first URL segment after core prefix. Should be unique to your package/plugin.
* @param string $route The base URL for route you are adding.
* @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for
* multiple methods. Default empty array.
* @param bool $override Optional. If the route already exists, should we override it? True overrides,
* false merges (with newer overriding if duplicate keys exist). Default false.
* @return bool True on success, false on error.
function register_rest_route( $route_namespace, $route, $args = array(), $override = false ) {
if ( empty( $route_namespace ) ) {
* Non-namespaced routes are not allowed, with the exception of the main
* and namespace indexes. If you really need to register a
* non-namespaced route, call `WP_REST_Server::register_route` directly.
/* translators: 1: string value of the namespace, 2: string value of the route. */
__( 'Routes must be namespaced with plugin or theme name and version. Instead there seems to be an empty namespace \'%1$s\' for route \'%2$s\'.' ),
'<code>' . $route_namespace . '</code>',
'<code>' . $route . '</code>'
} elseif ( empty( $route ) ) {
/* translators: 1: string value of the namespace, 2: string value of the route. */
__( 'Route must be specified. Instead within the namespace \'%1$s\', there seems to be an empty route \'%2$s\'.' ),
'<code>' . $route_namespace . '</code>',
'<code>' . $route . '</code>'
$clean_namespace = trim( $route_namespace, '/' );
if ( $clean_namespace !== $route_namespace ) {
/* translators: 1: string value of the namespace, 2: string value of the route. */
__( 'Namespace must not start or end with a slash. Instead namespace \'%1$s\' for route \'%2$s\' seems to contain a slash.' ),
'<code>' . $route_namespace . '</code>',
'<code>' . $route . '</code>'
if ( ! did_action( 'rest_api_init' ) ) {
/* translators: 1: rest_api_init, 2: string value of the route, 3: string value of the namespace. */
__( 'REST API routes must be registered on the %1$s action. Instead route \'%2$s\' with namespace \'%3$s\' was not registered on this action.' ),
'<code>rest_api_init</code>',
'<code>' . $route . '</code>',
'<code>' . $route_namespace . '</code>'
if ( isset( $args['args'] ) ) {
$common_args = $args['args'];
if ( isset( $args['callback'] ) ) {
// Upgrade a single set to multiple.
foreach ( $args as $key => &$arg_group ) {
if ( ! is_numeric( $key ) ) {
// Route option, skip here.
$arg_group = array_merge( $defaults, $arg_group );
$arg_group['args'] = array_merge( $common_args, $arg_group['args'] );
if ( ! isset( $arg_group['permission_callback'] ) ) {
/* translators: 1: The REST API route being registered, 2: The argument name, 3: The suggested function name. */
__( 'The REST API route definition for %1$s is missing the required %2$s argument. For REST API routes that are intended to be public, use %3$s as the permission callback.' ),
'<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>',
'<code>permission_callback</code>',
'<code>__return_true</code>'
foreach ( $arg_group['args'] as $arg ) {
if ( ! is_array( $arg ) ) {
/* translators: 1: $args, 2: The REST API route being registered. */
__( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s.' ),
'<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>'
break; // Leave the foreach loop once a non-array argument was found.
$full_route = '/' . $clean_namespace . '/' . trim( $route, '/' );
rest_get_server()->register_route( $clean_namespace, $full_route, $args, $override );
* Registers a new field on an existing WordPress object type.
* @global array $wp_rest_additional_fields Holds registered fields, organized
* @param string|array $object_type Object(s) the field is being registered to,
* "post"|"term"|"comment" etc.
* @param string $attribute The attribute name.
* Optional. An array of arguments used to handle the registered field.
* @type callable|null $get_callback Optional. The callback function used to retrieve the field value. Default is
* 'null', the field will not be returned in the response. The function will
* be passed the prepared object data.
* @type callable|null $update_callback Optional. The callback function used to set and update the field value. Default
* is 'null', the value cannot be set or updated. The function will be passed
* the model object, like WP_Post.
* @type array|null $schema Optional. The schema for this field.
* Default is 'null', no schema entry will be returned.
function register_rest_field( $object_type, $attribute, $args = array() ) {
global $wp_rest_additional_fields;
'update_callback' => null,
$args = wp_parse_args( $args, $defaults );
$object_types = (array) $object_type;
foreach ( $object_types as $object_type ) {
$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;
* Registers rewrite rules for the REST API.
* @see rest_api_register_rewrites()
* @global WP $wp Current WordPress environment instance.
function rest_api_init() {
rest_api_register_rewrites();
$wp->add_query_var( 'rest_route' );
* Adds REST rewrite rules.
* @see add_rewrite_rule()
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
function rest_api_register_rewrites() {
add_rewrite_rule( '^' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' );
add_rewrite_rule( '^' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' );
add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' );
add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' );
* Registers the default REST API filters.
* Attached to the {@see 'rest_api_init'} action
* to make testing and disabling these filters easier.
function rest_api_default_filters() {
if ( wp_is_serving_rest_request() ) {
add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 );
add_filter( 'deprecated_function_trigger_error', '__return_false' );
add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 );
add_filter( 'deprecated_argument_trigger_error', '__return_false' );
add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 );
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 );
add_filter( 'rest_index', 'rest_add_application_passwords_to_index' );
* Registers default REST API routes.
function create_initial_rest_routes() {
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
$controller = $post_type->get_rest_controller();
if ( ! $post_type->late_route_registration ) {
$controller->register_routes();
$revisions_controller = $post_type->get_revisions_rest_controller();
if ( $revisions_controller ) {
$revisions_controller->register_routes();
$autosaves_controller = $post_type->get_autosave_rest_controller();
if ( $autosaves_controller ) {
$autosaves_controller->register_routes();
if ( $post_type->late_route_registration ) {
$controller->register_routes();
$controller = new WP_REST_Post_Types_Controller();
$controller->register_routes();
$controller = new WP_REST_Post_Statuses_Controller();
$controller->register_routes();
$controller = new WP_REST_Taxonomies_Controller();
$controller->register_routes();
foreach ( get_taxonomies( array( 'show_in_rest' => true ), 'object' ) as $taxonomy ) {
$controller = $taxonomy->get_rest_controller();
$controller->register_routes();
$controller = new WP_REST_Users_Controller();
$controller->register_routes();
$controller = new WP_REST_Application_Passwords_Controller();
$controller->register_routes();
$controller = new WP_REST_Comments_Controller();
$controller->register_routes();
$search_handlers = array(
new WP_REST_Post_Search_Handler(),
new WP_REST_Term_Search_Handler(),
new WP_REST_Post_Format_Search_Handler(),
* Filters the search handlers to use in the REST search controller.
* @param array $search_handlers List of search handlers to use in the controller. Each search
* handler instance must extend the `WP_REST_Search_Handler` class.
* Default is only a handler for posts.
$search_handlers = apply_filters( 'wp_rest_search_handlers', $search_handlers );
$controller = new WP_REST_Search_Controller( $search_handlers );
$controller->register_routes();
$controller = new WP_REST_Block_Renderer_Controller();
$controller->register_routes();
$controller = new WP_REST_Block_Types_Controller();
$controller->register_routes();
$controller = new WP_REST_Settings_Controller();
$controller->register_routes();
$controller = new WP_REST_Themes_Controller();
$controller->register_routes();
$controller = new WP_REST_Plugins_Controller();
$controller->register_routes();
$controller = new WP_REST_Sidebars_Controller();
$controller->register_routes();
$controller = new WP_REST_Widget_Types_Controller();
$controller->register_routes();
$controller = new WP_REST_Widgets_Controller();
$controller->register_routes();
$controller = new WP_REST_Block_Directory_Controller();
$controller->register_routes();
$controller = new WP_REST_Pattern_Directory_Controller();
$controller->register_routes();
$controller = new WP_REST_Block_Patterns_Controller();
$controller->register_routes();
// Block Pattern Categories.
$controller = new WP_REST_Block_Pattern_Categories_Controller();
$controller->register_routes();
$site_health = WP_Site_Health::get_instance();
$controller = new WP_REST_Site_Health_Controller( $site_health );
$controller->register_routes();
$controller = new WP_REST_URL_Details_Controller();
$controller->register_routes();
$controller = new WP_REST_Menu_Locations_Controller();
$controller->register_routes();
$controller = new WP_REST_Edit_Site_Export_Controller();
$controller->register_routes();
$controller = new WP_REST_Navigation_Fallback_Controller();
$controller->register_routes();
$font_collections_controller = new WP_REST_Font_Collections_Controller();
$font_collections_controller->register_routes();
$abilities_categories_controller = new WP_REST_Abilities_V1_Categories_Controller();
$abilities_categories_controller->register_routes();
$abilities_run_controller = new WP_REST_Abilities_V1_Run_Controller();
$abilities_run_controller->register_routes();
$abilities_list_controller = new WP_REST_Abilities_V1_List_Controller();
$abilities_list_controller->register_routes();
* @global WP $wp Current WordPress environment instance.
function rest_api_loaded() {
if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
// Return an error message if query_var is not a string.
if ( ! is_string( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
$rest_type_error = new WP_Error(
'rest_path_invalid_type',
__( 'The REST route parameter must be a string.' ),
wp_die( $rest_type_error );
* Whether this is a REST Request.
define( 'REST_REQUEST', true );
// Initialize the server.
$server = rest_get_server();
$route = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] );
$server->serve_request( $route );
* Retrieves the URL prefix for any API resource.
function rest_get_url_prefix() {
* Filters the REST URL prefix.
* @param string $prefix URL prefix. Default 'wp-json'.
return apply_filters( 'rest_url_prefix', 'wp-json' );
* Retrieves the URL to a REST endpoint on a site.
* Note: The returned URL is NOT escaped.
* @todo Check if this is even necessary
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.