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/rest-api/endpoints/class-wp-rest-posts-controller.php

    r51962 r51964  
    20612061
    20622062            foreach ( $taxonomies as $tax ) {
    2063                 $taxonomy_obj = get_taxonomy( $tax );
     2063                $taxonomy_route = rest_get_route_for_taxonomy_items( $tax );
    20642064
    20652065                // Skip taxonomies that are not public.
    2066                 if ( empty( $taxonomy_obj->show_in_rest ) ) {
     2066                if ( empty( $taxonomy_route ) ) {
    20672067                    continue;
    20682068                }
    2069 
    2070                 $tax_base = ! empty( $taxonomy_obj->rest_base ) ? $taxonomy_obj->rest_base : $tax;
    2071 
    20722069                $terms_url = add_query_arg(
    20732070                    'post',
    20742071                    $post->ID,
    2075                     rest_url( 'wp/v2/' . $tax_base )
     2072                    rest_url( $taxonomy_route )
    20762073                );
    20772074
Note: See TracChangeset for help on using the changeset viewer.