Skip to content
Blogger Zia

How to Remove the Category Slug From WordPress Permalinks

Category: Code Snippets

By default, WordPress adds “/category/” to every category URL on your site.

This means your category links look longer than they need to, and the extra segment can affect how search engines read your URL structure.

In this post, I’ve shared a code snippet that removes the category slug from WordPress permalinks and keeps the clean path working for both top-level categories and sub-categories.

Site owners who care about clean, readable URLs and want category links that match the style of their post permalinks will find this most useful.

When to Use This

Add this snippet if any of these situations match your setup:

When to Avoid This

Some setups conflict with custom rewrite rules, so check these before adding the snippet:

The Code Snippet

/* =========================================================
 * Rewrite rules for categories & sub-categories
 * ========================================================= */
function bloggerzia_category_rewrite_rules($rules) {
    $new_rules = array();

    // Get all categories (including empty ones)
    $categories = get_categories(array(
        'hide_empty' => false
    ));

    foreach ($categories as $category) {
        // Get the full hierarchical path (parent/child)
        $category_parents = get_category_parents($category->term_id, false, '/', true);
        $category_parents = rtrim($category_parents, '/'); // Remove trailing slash

        // Add feed rule for this category
        $new_rules[$category_parents . '/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] =
            'index.php?category_name=' . $category->slug . '&feed=$matches[1]';

        // Add pagination rule for this category
        $new_rules[$category_parents . '/page/?([0-9]{1,})/?$'] =
            'index.php?category_name=' . $category->slug . '&paged=$matches[1]';

        // Add basic category rule (without /category/ base)
        $new_rules[$category_parents . '/?$'] =
            'index.php?category_name=' . $category->slug;
    }

    // Merge new rules with existing rules
    return $new_rules + $rules;
}
add_filter('category_rewrite_rules', 'bloggerzia_category_rewrite_rules');


/* =========================================================
 * Adjust post permalinks to include primary category
 * ========================================================= */
function bloggerzia_post_link_category($permalink, $post) {
    $categories = get_the_category($post->ID);
    if (!empty($categories)) {
        // Take the first (primary) category
        $primary_category = $categories[0];
        // Replace %category% in the permalink with primary category slug
        $permalink = str_replace('%category%', $primary_category->slug, $permalink);
    }
    return $permalink;
}
add_filter('post_link', 'bloggerzia_post_link_category', 10, 2);


/* =========================================================
 * Remove '/category/' base from category permalinks
 * ========================================================= */
function bloggerzia_category_base_removal($link, $category) {
    // Replace '/category/' with '/'
    return preg_replace('@/category/@', '/', $link, 1);
}
add_filter('category_link', 'bloggerzia_category_base_removal', 10, 2);

How to Add This Code Snippet

Follow these steps to add the snippet to your site:

  1. Add it to your child theme’s functions.php file or a code snippet plugin.
  2. Paste the code and save the changes.
  3. Go to Settings, then Permalinks, and click Save Changes to flush the rewrite rules. Do this even if you change nothing on that page.
  4. Test your category URLs to confirm the “/category/” segment no longer appears.
  5. Test on a staging site before using it on your live site.

Note: Use a child theme or a code snippet plugin. Avoid editing core files. Test on a staging site before going live. This snippet may not work with all themes or plugins.