Make WordPress Core


Ignore:
Timestamp:
10/31/2021 11:15:10 PM (3 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support custom namespaces for custom post types.

While a custom post type can define a custom route by using the rest_base argument, a namespace of wp/v2 was assumed. This commit introduces support for a rest_namespace argument.

A new rest_get_route_for_post_type_items function has been introduced and the rest_get_route_for_post function updated to facilitate getting the correct route for custom post types.

While the WordPress Core Block Editor bootstrap code has been updated to use these API functions, for maximum compatibility sticking with the default wp/v2 namespace is recommended until the API functions see wider use.

Props spacedmonkey, swissspidy.
Fixes #53656.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-post-type.php

    r49790 r51962  
    358358     */
    359359    public $rest_base;
     360
     361    /**
     362     * The namespace for this post type's REST API endpoints.
     363     *
     364     * @since 5.9
     365     * @var string|bool $rest_namespace
     366     */
     367    public $rest_namespace;
    360368
    361369    /**
     
    453461            'show_in_rest'          => false,
    454462            'rest_base'             => false,
     463            'rest_namespace'        => false,
    455464            'rest_controller_class' => false,
    456465            'template'              => array(),
     
    472481        if ( null === $args['show_ui'] ) {
    473482            $args['show_ui'] = $args['public'];
     483        }
     484
     485        // If not set, default rest_namespace to wp/v2 if show_in_rest is true.
     486        if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
     487            $args['rest_namespace'] = 'wp/v2';
    474488        }
    475489
Note: See TracChangeset for help on using the changeset viewer.