rest_get_route_for_taxonomy_items() WordPress Function

The rest_get_route_for_taxonomy_items() function is used to get the REST route for a taxonomy term. It takes two arguments: the taxonomy name and the term name. This function is useful for registering custom routes for taxonomies. For example, if you have a custom taxonomy called "fruit", you can use this function to get the route for "fruit" terms.

rest_get_route_for_taxonomy_items( string $taxonomy ) #

Gets the REST API route for a taxonomy.


Parameters

$taxonomy

(string)(Required)Name of taxonomy.


Top ↑

Return

(string) The route path with a leading slash for the given taxonomy.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_get_route_for_taxonomy_items( $taxonomy ) {
	$taxonomy = get_taxonomy( $taxonomy );
	if ( ! $taxonomy ) {
		return '';
	}

	if ( ! $taxonomy->show_in_rest ) {
		return '';
	}

	$namespace = ! empty( $taxonomy->rest_namespace ) ? $taxonomy->rest_namespace : 'wp/v2';
	$rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
	$route     = sprintf( '/%s/%s', $namespace, $rest_base );

	/**
	 * Filters the REST API route for a taxonomy.
	 *
	 * @since 5.9.0
	 *
	 * @param string      $route    The route path.
	 * @param WP_Taxonomy $taxonomy The taxonomy object.
	 */
	return apply_filters( 'rest_route_for_taxonomy_items', $route, $taxonomy );
}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More