rest_get_route_for_term

The timeline below displays how wordpress function rest_get_route_for_term has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.9

/**
 * Gets the REST API route for a term.
 *
 * @since 5.5.0
 *
 * @param int|WP_Term $term Term ID or term object.
 * @return string The route path with a leading slash for the given term,
 *                or an empty string if there is not a route.
 */
function rest_get_route_for_term($term)
{
    $term = get_term($term);
    if (!$term instanceof WP_Term) {
        return '';
    }
    $taxonomy_route = rest_get_route_for_taxonomy_items($term->taxonomy);
    if (!$taxonomy_route) {
        return '';
    }
    $route = sprintf('%s/%d', $taxonomy_route, $term->term_id);
    /**
     * Filters the REST API route for a term.
     *
     * @since 5.5.0
     *
     * @param string  $route The route path.
     * @param WP_Term $term  The term object.
     */
    return apply_filters('rest_route_for_term', $route, $term);
}

WordPress Version: 5.6

/**
 * Gets the REST API route for a term.
 *
 * @since 5.5.0
 *
 * @param int|WP_Term $term Term ID or term object.
 * @return string The route path with a leading slash for the given term, or an empty string if there is not a route.
 */
function rest_get_route_for_term($term)
{
    $term = get_term($term);
    if (!$term instanceof WP_Term) {
        return '';
    }
    $taxonomy = get_taxonomy($term->taxonomy);
    if (!$taxonomy) {
        return '';
    }
    $controller = $taxonomy->get_rest_controller();
    if (!$controller) {
        return '';
    }
    $route = '';
    // The only controller that works is the Terms controller.
    if ($controller instanceof WP_REST_Terms_Controller) {
        $namespace = 'wp/v2';
        $rest_base = (!empty($taxonomy->rest_base)) ? $taxonomy->rest_base : $taxonomy->name;
        $route = sprintf('/%s/%s/%d', $namespace, $rest_base, $term->term_id);
    }
    /**
     * Filters the REST API route for a term.
     *
     * @since 5.5.0
     *
     * @param string  $route The route path.
     * @param WP_Term $term  The term object.
     */
    return apply_filters('rest_route_for_term', $route, $term);
}

WordPress Version: 5.5

/**
 * Gets the REST API route for a term.
 *
 * @since 5.5.0
 *
 * @param int|WP_Term $term Term ID or term object.
 * @return string The route path with a leading slash for the given term, or an empty string if there is not a route.
 */
function rest_get_route_for_term($term)
{
    $term = get_term($term);
    if (!$term instanceof WP_Term) {
        return '';
    }
    $taxonomy = get_taxonomy($term->taxonomy);
    if (!$taxonomy) {
        return '';
    }
    $controller = $taxonomy->get_rest_controller();
    if (!$controller) {
        return '';
    }
    $route = '';
    // The only controller that works is the Terms controller.
    if ('WP_REST_Terms_Controller' === get_class($controller)) {
        $namespace = 'wp/v2';
        $rest_base = (!empty($taxonomy->rest_base)) ? $taxonomy->rest_base : $taxonomy->name;
        $route = sprintf('/%s/%s/%d', $namespace, $rest_base, $term->term_id);
    }
    /**
     * Filters the REST API route for a term.
     *
     * @since 5.5.0
     *
     * @param string  $route The route path.
     * @param WP_Term $term  The term object.
     */
    return apply_filters('rest_route_for_term', $route, $term);
}