Make WordPress Core


Ignore:
Timestamp:
11/01/2021 03:26:06 AM (3 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support custom namespaces for taxonomies.

While a taxonomy 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_taxonomy_items function has been introduced and the rest_get_route_for_term function updated to facilitate getting the correct route for taxonomies.

For maximum compatibility sticking with the default wp/v2 namespace is recommended until the API functions see wider use.

Props spacedmonkey.
Fixes #54267.
See [51962].

File:
1 edited

Legend:

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

    r50119 r51964  
    199199     */
    200200    public $rest_base;
     201
     202    /**
     203     * The namespace for this taxonomy's REST API endpoints.
     204     *
     205     * @since 5.9
     206     * @var string|bool $rest_namespace
     207     */
     208    public $rest_namespace;
    201209
    202210    /**
     
    320328            'show_in_rest'          => false,
    321329            'rest_base'             => false,
     330            'rest_namespace'        => false,
    322331            'rest_controller_class' => false,
    323332            'default_term'          => null,
     
    383392        if ( null === $args['show_in_quick_edit'] ) {
    384393            $args['show_in_quick_edit'] = $args['show_ui'];
     394        }
     395
     396        // If not set, default rest_namespace to wp/v2 if show_in_rest is true.
     397        if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
     398            $args['rest_namespace'] = 'wp/v2';
    385399        }
    386400
Note: See TracChangeset for help on using the changeset viewer.