* 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 $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( $namespace, $route, $args = array(), $override = false ) {
if ( empty( $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.
_doing_it_wrong( 'register_rest_route', __( 'Routes must be namespaced with plugin or theme name and version.' ), '4.4.0' );
} elseif ( empty( $route ) ) {
_doing_it_wrong( 'register_rest_route', __( 'Route must be specified.' ), '4.4.0' );
$clean_namespace = trim( $namespace, '/' );
if ( $clean_namespace !== $namespace ) {
_doing_it_wrong( __FUNCTION__, __( 'Namespace must not start or end with a slash.' ), '5.4.2' );
if ( ! did_action( 'rest_api_init' ) ) {
/* translators: %s: rest_api_init */
__( 'REST API routes must be registered on the %s action.' ),
'<code>rest_api_init</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>'
$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() ) {
'update_callback' => null,
$args = wp_parse_args( $args, $defaults );
global $wp_rest_additional_fields;
$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 ( defined( 'REST_REQUEST' ) && 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();
$controller->register_routes();
if ( post_type_supports( $post_type->name, 'revisions' ) ) {
$revisions_controller = new WP_REST_Revisions_Controller( $post_type->name );
$revisions_controller->register_routes();
if ( 'attachment' !== $post_type->name ) {
$autosaves_controller = new WP_REST_Autosaves_Controller( $post_type->name );
$autosaves_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_Block_Directory_Controller();
$controller->register_routes();
$site_health = WP_Site_Health::get_instance();
$controller = new WP_REST_Site_Health_Controller( $site_health );
$controller->register_routes();
* @global WP $wp Current WordPress environment instance.
function rest_api_loaded() {
if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
* 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.
* @param int|null $blog_id Optional. Blog ID. Default of null returns URL for current blog.
* @param string $path Optional. REST route. Default '/'.
* @param string $scheme Optional. Sanitization scheme. Default 'rest'.
* @return string Full URL to the endpoint.
function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
$path = '/' . ltrim( $path, '/' );
if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
if ( $wp_rewrite->using_index_permalinks() ) {
$url = get_home_url( $blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme );
$url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
// nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
// To work around this, we manually add index.php to the URL, avoiding the redirect.
if ( 'index.php' !== substr( $url, 9 ) ) {
$url = add_query_arg( 'rest_route', $path, $url );
if ( is_ssl() && isset( $_SERVER['SERVER_NAME'] ) ) {
// If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
if ( parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) === $_SERVER['SERVER_NAME'] ) {
$url = set_url_scheme( $url, 'https' );
if ( is_admin() && force_ssl_admin() ) {
* In this situation the home URL may be http:, and `is_ssl()` may be false,
* but the admin is served over https: (one way or another), so REST API usage
* will be blocked by browsers unless it is also served over HTTPS.
$url = set_url_scheme( $url, 'https' );
* Use this filter to adjust the url returned by the get_rest_url() function.
* @param string $url REST URL.
* @param string $path REST route.
* @param int|null $blog_id Blog ID.
* @param string $scheme Sanitization scheme.
return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
* Retrieves the URL to a REST endpoint.
* Note: The returned URL is NOT escaped.
* @param string $path Optional. REST route. Default empty.
* @param string $scheme Optional. Sanitization scheme. Default 'rest'.
* @return string Full URL to the endpoint.
function rest_url( $path = '', $scheme = 'rest' ) {
return get_rest_url( null, $path, $scheme );
* Used primarily to route internal requests through WP_REST_Server.
* @param WP_REST_Request|string $request Request.
* @return WP_REST_Response REST response.
function rest_do_request( $request ) {
$request = rest_ensure_request( $request );
return rest_get_server()->dispatch( $request );
* Retrieves the current REST server instance.
* Instantiates a new instance if none exists already.
* @global WP_REST_Server $wp_rest_server REST server instance.
* @return WP_REST_Server REST server instance.
function rest_get_server() {
/* @var WP_REST_Server $wp_rest_server */
if ( empty( $wp_rest_server ) ) {
* Filters the REST Server Class.