Using WordPress ‘redirect_term_location’ PHP filter

apply_filters( ‘redirect_term_location’, string $location, WP_Taxonomy $tax ) is a WordPress PHP filter that allows you to modify the destination URL when redirecting to a taxonomy term page.

Usage

To use this filter, you need to create a custom function that modifies the destination URL and then hook it to the ‘redirect_term_location’ filter. Here’s a code example:

function my_custom_redirect_term_location( $location, $tax ) {
    // Your code to modify the $location
    return $location;
}
add_filter( 'redirect_term_location', 'my_custom_redirect_term_location', 10, 2 );

Parameters

  • $location (string)
    • The destination URL.
  • $tax (WP_Taxonomy)
    • The taxonomy object.

Examples

Add UTM Parameters

Scenario: You want to add UTM parameters to the destination URL for tracking purposes.

function add_utm_redirect_term_location( $location, $tax ) {
    $location = add_query_arg( 'utm_source', 'my-source', $location );
    $location = add_query_arg( 'utm_medium', 'my-medium', $location );
    $location = add_query_arg( 'utm_campaign', 'my-campaign', $location );
    return $location;
}
add_filter( 'redirect_term_location', 'add_utm_redirect_term_location', 10, 2 );

Explanation: This code adds UTM parameters to the destination URL, which can be used for tracking the source, medium, and campaign of your taxonomy term page.

Redirect to a Custom URL

Scenario: You want to redirect the user to a custom URL instead of the taxonomy term page.

function custom_url_redirect_term_location( $location, $tax ) {
    if ( 'my_custom_taxonomy' === $tax->name ) {
        $location = 'https://example.com/my-custom-url';
    }
    return $location;
}
add_filter( 'redirect_term_location', 'custom_url_redirect_term_location', 10, 2 );

Explanation: This code checks if the taxonomy is ‘my_custom_taxonomy’ and changes the destination URL to a custom URL if it is.

Add a Hash to the URL

Scenario: You want to add a specific hash to the destination URL.

function add_hash_redirect_term_location( $location, $tax ) {
    $location .= '#my-hash';
    return $location;
}
add_filter( 'redirect_term_location', 'add_hash_redirect_term_location', 10, 2 );

Explanation: This code appends a hash (‘#my-hash’) to the destination URL.

Redirect to a Different Taxonomy Term

Scenario: You want to redirect the user to a different taxonomy term.

function different_term_redirect_term_location( $location, $tax ) {
    if ( 'category' === $tax->name ) {
        $new_term_id = 25;
        $location = get_term_link( $new_term_id, $tax->name );
    }
    return $location;
}
add_filter( 'redirect_term_location', 'different_term_redirect_term_location', 10, 2 );

Explanation: This code checks if the taxonomy is ‘category’ and changes the destination URL to another term with an ID of 25.

Add a Query Parameter

Scenario: You want to add a query parameter to the destination URL.

function add_query_param_redirect_term_location( $location, $tax ) {
    $location = add_query_arg( 'my_param', 'my_value', $location ); 
return $location; 
} 
add_filter( 'redirect_term_location', 'add_query_param_redirect_term_location', 10, 2 );

Explanation: This code adds a query parameter (‘my_param’) with a value (‘my_value’) to the destination URL.