Last updated on September 25, 2023

Change the Default Category Base for Custom Taxonomies

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

Organize category base for taxonomy.

To change the default category base for custom taxonomies in WordPress, you can use the register_taxonomy() function and set the rewrite parameter to customize the permalink structure.

Here’s an example code snippet that demonstrates how to change the default category base for a custom taxonomy named “product_category”:

function wpsnippets_change_taxonomy_category_base() {
    $taxonomy = 'product_category';
    $taxonomy_slug = 'products'; // Replace with your desired category base

    $args = array(
        'rewrite' => array(
            'slug' => $taxonomy_slug,
            'with_front' => false,
        ),
    );

    register_taxonomy( $taxonomy, array( 'product' ), $args );
}
add_action( 'init', 'wpsnippets_change_taxonomy_category_base' );

In this example, we define a custom taxonomy named “product_category” and set the rewrite parameter to specify the desired category base slug as “products”. The with_front parameter is set to false to remove the front base from the permalink structure.

Remember to replace 'product_category' with the actual taxonomy name you want to modify, and 'products' with your desired category base slug.

This code snippet can be useful when you want to customize the permalink structure for your custom taxonomies, providing a more meaningful and SEO-friendly URL for your taxonomy archive pages.

Examples

Example 1: Changing the default category base for a custom taxonomy

This example demonstrates how to change the default category base for a custom taxonomy in WordPress. By default, the category base for custom taxonomies is set to “category”, but you can customize it to something else like “topics” or “subjects”.

function wpsnippets_change_taxonomy_category_base() {
    global $wp_rewrite;
    $wp_rewrite->taxonomy_base = 'topics';
    $wp_rewrite->flush_rules();
}
add_action( 'init', 'wpsnippets_change_taxonomy_category_base' );

In this example, we define a custom function wpsnippets_change_taxonomy_category_base that modifies the taxonomy_base property of the global $wp_rewrite object to the desired value, in this case, ‘topics’. We then hook this function to the init action using the add_action function. Finally, we call the flush_rules method of the $wp_rewrite object to regenerate the rewrite rules and apply the changes.

Example 2: Changing the default category base for multiple custom taxonomies

This example demonstrates how to change the default category base for multiple custom taxonomies in WordPress. You can use this approach if you have multiple taxonomies and want to customize their category bases individually.

function wpsnippets_change_taxonomy_category_base() {
    global $wp_rewrite;

    $taxonomy_bases = array(
        'topics' => 'subjects',
        'genres' => 'categories',
    );

    foreach ( $taxonomy_bases as $taxonomy => $base ) {
        $wp_rewrite->extra_permastructs[ $taxonomy ]['struct'] = "{$base}/%$taxonomy%";
    }

    $wp_rewrite->flush_rules();
}
add_action( 'init', 'wpsnippets_change_taxonomy_category_base' );

In this example, we define a custom function wpsnippets_change_taxonomy_category_base that modifies the extra_permastructs property of the global $wp_rewrite object. We use an associative array to map each taxonomy to its desired category base. Inside the foreach loop, we update the struct property of each taxonomy’s permastruct to include the new category base. Finally, we call the flush_rules method of the $wp_rewrite object to regenerate the rewrite rules and apply the changes.

Example 3: Changing the default category base for a specific custom taxonomy

This example demonstrates how to change the default category base for a specific custom taxonomy in WordPress. You can use this approach if you only want to customize the category base for a specific taxonomy and leave the others unchanged.

function wpsnippets_change_taxonomy_category_base() {
    global $wp_rewrite;

    $taxonomy = 'topics';
    $base = 'subjects';

    $wp_rewrite->extra_permastructs[ $taxonomy ]['struct'] = "{$base}/%$taxonomy%";

    $wp_rewrite->flush_rules();
}
add_action( 'init', 'wpsnippets_change_taxonomy_category_base' );

In this example, we define a custom function wpsnippets_change_taxonomy_category_base that modifies the extra_permastructs property of the global $wp_rewrite object. We specify the taxonomy we want to customize ($taxonomy) and the desired category base ($base). We then update the struct property of the taxonomy’s permastruct to include the new category base. Finally, we call the flush_rules method of the $wp_rewrite object to regenerate the rewrite rules and apply the changes.

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

Leave a Reply

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