Last updated on September 25, 2023

Remove the “Category” Base from Custom Taxonomy URLs

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

Cleaner custom taxonomy URLs.

To remove the “Category” base from custom taxonomy URLs in WordPress, you can use the rewrite_rules_array filter along with the add_rewrite_rule function. This code snippet is useful when you want to create cleaner and more user-friendly URLs for your custom taxonomies.

Here’s an example code snippet that demonstrates how to remove the “Category” base from custom taxonomy URLs:

function wpsnippets_remove_category_base() {
    $taxonomy = 'your_taxonomy_slug';
    $taxonomy_object = get_taxonomy($taxonomy);

    if ($taxonomy_object->rewrite['hierarchical'] === true) {
        $taxonomy_slug = $taxonomy_object->rewrite['slug'];
        add_rewrite_rule(
            $taxonomy_slug . '/(.+?)/?$',
            'index.php?' . $taxonomy . '=$matches[1]',
            'top'
        );
    }
}
add_action('init', 'wpsnippets_remove_category_base');

In the code snippet above, replace 'your_taxonomy_slug' with the actual slug of your custom taxonomy. This code checks if the custom taxonomy is hierarchical (like categories) and then adds a rewrite rule to remove the “Category” base from the URL structure. The add_rewrite_rule function is used to define the new URL structure.

Remember to flush the rewrite rules after adding this code snippet. You can do this by visiting the “Settings” > “Permalinks” page in your WordPress admin area and simply clicking the “Save Changes” button.

Note: Make sure to replace 'your_taxonomy_slug' with the actual slug of your custom taxonomy in the code snippet.

This code snippet can be useful when you want to create cleaner and more user-friendly URLs for your custom taxonomies. It removes the “Category” base from the URL structure, making the URLs shorter and more descriptive.

Examples

Example 1: Removing the “Category” base from custom taxonomy URLs using register_taxonomy

This example demonstrates how to remove the “Category” base from custom taxonomy URLs by using the register_taxonomy function with the rewrite parameter.

function wpsnippets_remove_category_base() {
    $taxonomy = 'your_taxonomy';
    $args = array(
        'rewrite' => array(
            'slug' => 'your_taxonomy',
            'with_front' => false,
        ),
    );
    register_taxonomy( $taxonomy, array( 'your_post_type' ), $args );
}
add_action( 'init', 'wpsnippets_remove_category_base' );

In this code example, we define a custom taxonomy called 'your_taxonomy' and set the 'rewrite' parameter to remove the “Category” base from the taxonomy URLs. The 'slug' is set to 'your_taxonomy' and 'with_front' is set to false. Finally, we register the taxonomy using the register_taxonomy function.

Example 2: Removing the “Category” base from custom taxonomy URLs using register_taxonomy_args

This example demonstrates how to remove the “Category” base from custom taxonomy URLs by modifying the register_taxonomy_args filter.

function wpsnippets_remove_category_base( $args, $taxonomy ) {
    if ( $taxonomy === 'your_taxonomy' ) {
        $args['rewrite']['slug'] = 'your_taxonomy';
        $args['rewrite']['with_front'] = false;
    }
    return $args;
}
add_filter( 'register_taxonomy_args', 'wpsnippets_remove_category_base', 10, 2 );

In this code example, we define a function wpsnippets_remove_category_base that modifies the $args array for the custom taxonomy 'your_taxonomy'. We set the 'slug' to 'your_taxonomy' and 'with_front' to false. Finally, we use the register_taxonomy_args filter to apply the modifications.

Example 3: Removing the “Category” base from custom taxonomy URLs using rewrite_rules_array

This example demonstrates how to remove the “Category” base from custom taxonomy URLs by modifying the rewrite rules directly using the rewrite_rules_array filter.

function wpsnippets_remove_category_base( $rules ) {
    $new_rules = array();
    foreach ( $rules as $rule => $rewrite ) {
        if ( preg_match( '/your_taxonomy/(.+?)(?:/page/(d+))?/?$/', $rule, $matches ) ) {
            $new_rule = 'index.php?your_taxonomy=' . $matches[1] . '&paged=' . $matches[2];
            $new_rules[ $new_rule ] = $rewrite;
        }
    }
    return $new_rules + $rules;
}
add_filter( 'rewrite_rules_array', 'wpsnippets_remove_category_base' );

In this code example, we define a function wpsnippets_remove_category_base that modifies the rewrite rules by iterating through each rule and checking if it matches the pattern for 'your_taxonomy' URLs. If a match is found, we create a new rule that removes the “Category” base and adds the 'your_taxonomy' query variable. Finally, we return the modified rules by adding our new rules to the beginning of the array using the + operator.

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

Leave a Reply