Last updated on September 13, 2023

Remove Category from URL / Permalink

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

Remove the /category/ base from category archive URLs / permalinks.

By default, WordPress adds the category slug as a base to permalinks/URLs of the category archive pages: /category/. However, you may want to remove this category base from the URL / Permalink to make your URLs cleaner to read and therefore more user-friendly. This turns example.com/category/recipes into example.com/recipes. To achieve this functionality, you can use the following code snippet:

function wpsnippets_remove_category_base() {

    global $wp_rewrite;

    // Remove category base
    $wp_rewrite->category_base = '';

    // Remove the prefix from category permalinks
    $wp_rewrite->permalink_structure = str_replace( '/category/' , '/' , $wp_rewrite->permalink_structure );
}

add_action('init', 'wpsnippets_remove_category_base');

Please note: After adding this code snippet, you need to flush your rewrite rules. You can do this by going to Settings → Permalinks and just click on Save changes. After this, your category permalinks/URLs don’t contain the /category/ base anymore.

This code removes the /category/ base from category archive URLs and replaces it with a forward slash /. It does this by setting the global $wp_rewrite->category_base property to an empty string, and then modifying the permalink structure to remove the /category/ string.

Make sure that the slugs of your categories don’t conflict with slugs of posts or pages, because they now use the same rewrite rules.

This functionality can be useful if you want to create more user-friendly and memorable URLs for your categories, or if you want to simplify your site’s URL structure.

Last updated on September 13, 2023. Originally posted on May 5, 2023.

Leave a Reply

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