Last updated on September 25, 2023

Remove the “Tag” Base from Tag Archive URLs

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Clean tag archive URLs.

To remove the “Tag” base from tag archive URLs in WordPress, you can use the rewrite_rules_array filter hook along with the add_rewrite_rule() function. This code snippet will modify the rewrite rules to remove the “tag” base from the tag archive URLs.

function wpsnippets_remove_tag_base() {
    // Remove the "tag" base from tag archive URLs
    add_rewrite_rule(
        '^tag/([^/]+)/?$',
        'index.php?tag=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpsnippets_remove_tag_base' );

This code should be added to your theme’s functions.php file or in a custom plugin. After adding the code, you need to flush the rewrite rules for the changes to take effect. You can do this by visiting the “Settings” > “Permalinks” page in your WordPress admin area and simply clicking the “Save Changes” button.

Once the code is implemented and the rewrite rules are flushed, the “tag” base will be removed from the tag archive URLs. For example, instead of example.com/tag/tag-name, the URL will be example.com/tag-name.

This code snippet is useful when you want to customize the URL structure of your WordPress site and make it more user-friendly. It helps to create cleaner and more concise URLs for tag archives, improving the overall SEO and user experience of your website.

Examples

Example 1: Removing the “Tag” base from tag archive URLs using the tag_rewrite_rules filter.

This code example demonstrates how to remove the “tag” base from tag archive URLs in WordPress. By modifying the rewrite rules for tags, we can achieve cleaner and more user-friendly URLs.

function wpsnippets_remove_tag_base( $rules ) {
    $new_rules = array();
    foreach ( $rules as $rule => $rewrite ) {
        $new_rule = str_replace( 'tag/', '', $rule );
        $new_rules[ $new_rule ] = $rewrite;
    }
    return $new_rules;
}
add_filter( 'tag_rewrite_rules', 'wpsnippets_remove_tag_base' );

This code snippet uses the tag_rewrite_rules filter to modify the rewrite rules for tag archives. It loops through each rule and removes the “tag/” base from the rule. The modified rules are then returned, effectively removing the “tag” base from tag archive URLs.

Example 2: Removing the “Tag” base from tag archive URLs using the rewrite_rules_array filter.

This code example demonstrates an alternative method to remove the “tag” base from tag archive URLs. Instead of using the tag_rewrite_rules filter, we can use the rewrite_rules_array filter to achieve the same result.

function wpsnippets_remove_tag_base( $rules ) {
    foreach ( $rules as $rule => $rewrite ) {
        if ( strpos( $rule, 'tag/' ) === 0 ) {
            unset( $rules[ $rule ] );
        }
    }
    return $rules;
}
add_filter( 'rewrite_rules_array', 'wpsnippets_remove_tag_base' );

In this code snippet, we use the rewrite_rules_array filter to modify the rewrite rules. It iterates through each rule and checks if it starts with “tag/”. If it does, the rule is removed from the array of rules. The modified rules are then returned, effectively removing the “tag” base from tag archive URLs.

Example 3: Removing the “Tag” base from tag archive URLs using the register_taxonomy_args filter.

This code example demonstrates another approach to remove the “tag” base from tag archive URLs. Instead of modifying the rewrite rules directly, we can modify the arguments of the post_tag taxonomy using the register_taxonomy_args filter.

function wpsnippets_remove_tag_base( $args, $taxonomy ) {
    if ( $taxonomy === 'post_tag' ) {
        $args['rewrite']['slug'] = '';
    }
    return $args;
}
add_filter( 'register_taxonomy_args', 'wpsnippets_remove_tag_base', 10, 2 );

In this code snippet, we use the register_taxonomy_args filter to modify the arguments of the post_tag taxonomy. If the taxonomy being registered is post_tag, we set the slug argument to an empty string. This effectively removes the “tag” base from tag archive URLs.

Last updated on September 25, 2023. Originally posted on October 6, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *