You are currently viewing Remove Category Slug From WordPress URLs Without Plugins

Remove Category Slug From WordPress URLs Without Plugins

Today, I want to talk about a simple yet effective way to make your WordPress website’s URLs look clean and tidy.

Have you ever noticed those extra words, like “category” or “product-category,” that show up in your website’s URLs? Well, I’m here to show you how to remove them without using any fancy plugins.

By doing this, you’ll not only make your URLs shorter and more user-friendly but also potentially improve your website’s search engine optimization (SEO).

So, let’s dive in and learn how to remove category slugs from WordPress URLs!

Understanding Category Slugs in WordPress URLs

First things first, let’s understand what category slugs are and why they exist.

In WordPress, category slugs are those additional words that appear in the URLs of your category and product category pages.

Remove Category Slug From WordPress URLs Without Plugins 1

For example, if you have a category called “Beginner’s Guide,” the default URL would look something like “yourdomain.com/category/beginners-guide/”.

Remove Category Slug From WordPress URLs Without Plugins 2

Similarly, for a product category like “Smartphones,” the URL might be “yourdomain.com/product-category/smartphones/”.

These category slugs help organize your website’s content and provide a structure for your URLs.

Benefits of Removing Category Slugs

Removing category slugs from your WordPress URLs offers several fantastic benefits for your website. Let’s take a look at them:

  • Cleaner and more user-friendly URLs: By eliminating category slugs, your URLs become shorter and easier to read and remember. Users will appreciate the simplicity and clarity of your website’s links.
  • Improved SEO and keyword prominence: Removing category slugs bring your important keywords closer to the domain name in the URL structure. This helps search engines understand the relevance of your content and can potentially improve your rankings in search results.
  • Higher click-through rates: Short and keyword-rich URLs tend to attract more clicks from users in search engine listings. When your URL clearly showcases the content topic, users are more likely to click on it, resulting in increased traffic to your website.
  • Enhanced user experience: Clean URLs contribute to a better user experience by providing concise and descriptive links. Visitors can easily discern the topic of the page they’re about to visit, which can lead to higher engagement and reduced bounce rates.
  • Simplified sharing and link building: When sharing your website’s URLs on social media platforms or in external publications, shorter and cleaner URLs are more aesthetically pleasing and easier to share. They also tend to be more preferred for link-building purposes.

By leveraging these benefits, you can create a more polished and professional online presence while potentially improving your website’s performance in terms of search engine rankings and user engagement.

Now, let’s move on to the step-by-step guide to removing category slugs from your WordPress URLs.

Removing Category Slugs for Blog Post Categories

Alright, let’s get down to business and remove those pesky category slugs from your blog post URLs. Follow these simple steps, and you’ll have cleaner URLs in no time:

Step 1: Accessing the functions.php file:

To begin, we need to access the functions.php file in your WordPress theme. Don’t worry, it’s not as technical as it sounds! Here’s how you can do it:

Remove Category Slug From WordPress URLs Without Plugins 3
  • Log in to your WordPress dashboard.
  • Navigate to “Appearance” and select “Theme Editor.”
  • On the right-hand side, you’ll see a list of theme files. Look for “Theme Functions (functions.php)” and click on it.

Step 2: Adding the necessary code snippet:

Now that you’re in the functions.php file, it’s time to add a code snippet that will remove the category slugs. Here’s the code you need to insert:

// Flush rewrite rules for custom post types
add_action('after_switch_theme', 'flush_rewrite_rules');

// Rewrite rule for category base
add_filter('category_rewrite_rules', function($category_rewrite_rules) {
    $category_rewrite_rules = array();
    $categories = get_categories(array('hide_empty'=>false));
    foreach ($categories as $category) {
        $category_nicename = $category->slug;
        $category_rewrite_rules['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
        $category_rewrite_rules['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
        $category_rewrite_rules['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
    }
    return $category_rewrite_rules;
});

// Modify category permalink
add_filter('post_link_category', function($permalink, $category) {
    $categories = get_the_category();
    if ($categories) {
        usort($categories, '_usort_terms_by_ID');
        $category = $categories[0];
        $permalink = str_replace('%category%', $category->slug, $permalink);
    }
    return $permalink;
}, 10, 2);

// Remove category base from permalinks
add_filter('category_link', function($link, $category) {
    if ($category->parent == 0) {
        $link = str_replace('/category/', '/', $link);
    }
    return $link;
}, 10, 2);

Step 3: Saving the changes and updating the permalinks structure:

Great! You’ve added the code snippet to remove the category slugs. Now, it’s time to save the changes and update your permalinks structure. Follow these steps:

  • Scroll down to the bottom of the functions.php file.
  • Click on the “Update File” button to save your changes.

Now, let’s update the permalinks structure:

Remove Category Slug From WordPress URLs Without Plugins 4
  • Go to your WordPress dashboard and navigate to “Settings” and select “Permalinks.”
  • You don’t need to make any changes on this page. Simply click on the “Save Changes” button to update your permalinks structure.

Testing and verifying the removal of category slugs:

Congratulations! You’ve successfully removed category slugs from your blog post URLs. To ensure everything is working correctly, follow these steps to test it out:

Remove Category Slug From WordPress URLs Without Plugins 5
  • Visit any of your blog post category pages and check the URL. You should see that the “/category/” part has been removed.
  • Click on the blog post links and confirm that they lead to the correct posts without the category slug in the URL.

That’s it! You’ve now removed category slugs from your blog post URLs. Enjoy your cleaner and more user-friendly links.

Removing Category Slugs for Product Categories

Now, let’s move on to removing category slugs for product categories. Follow these simple steps to clean up your product category URLs:

Step 1: Accessing the functions.php file:

Just like we did for blog post categories, we need to access the functions.php file in your WordPress theme. Here’s a quick reminder of how to get there:

Remove Category Slug From WordPress URLs Without Plugins 6
  • Log in to your WordPress dashboard.
  • Navigate to “Appearance” and select “Theme Editor.”
  • Look for the “Theme Functions (functions.php)” file on the right-hand side and click on it.

Step 2: Adding the code snippet for product categories:

Now, let’s add the code snippet that will remove the category slugs specifically for product categories. Insert the following code into the functions.php file:

// Flush rewrite rules for custom post types
add_action('after_switch_theme', 'bzian_flush_rewrite_rules_for_product_category');

// Modify category permalink
add_filter('term_link', function($permalink, $term, $taxonomy) {
    if ($taxonomy === 'product_cat') {
        $permalink = str_replace('/product-category', '', $permalink);
    }
    return $permalink;
}, 10, 3);

// Remove product-category base from permalinks
add_filter('woocommerce_get_permalink', function($link, $product) {
    $terms = wc_get_product_terms($product->get_id(), 'product_cat', array('orderby' => 'parent', 'order' => 'DESC'));
    if ($terms && !is_wp_error($terms)) {
        $main_term = $terms[0];
        if ($main_term->parent == 0) {
            $link = str_replace('/product-category/', '/', $link);
        }
    }
    return $link;
}, 10, 2);

// Rewrite rule for product category base
add_filter('rewrite_rules_array', function($rules) {
    $new_rules = array();
    $terms = get_terms(array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false,
    ));
    foreach ($terms as $term) {
        $term_slug = $term->slug;
        $new_rules['^'.$term_slug.'(/.*)?$'] = 'index.php?product_cat='.$term_slug.'&$matches[1]';
    }
    return $new_rules + $rules;
});

Step 3: Saving the changes and updating the permalinks structure:

You’re almost there! Now, let’s save the changes you made to the functions.php file and update the permalinks structure. Here’s what you need to do:

  • Scroll down to the bottom of the functions.php file.
  • Click on the “Update File” button to save your changes.

Now, let’s update the permalinks structure:

Remove Category Slug From WordPress URLs Without Plugins 7
  • Go to your WordPress dashboard and navigate to “Settings” and select “Permalinks.”
  • No need to make any changes on this page. Simply click on the “Save Changes” button to update your permalinks structure.

Testing and verifying the removal of category slugs:

Congratulations! You’ve successfully removed category slugs from your product category URLs. Let’s make sure everything is working as expected:

Remove Category Slug From WordPress URLs Without Plugins 8
  • Visit any of your product category pages and check the URL. You should see that the “/product-category/” part has been removed.
  • Click on the product category links and verify that they lead to the correct product categories without the category slug in the URL.

Great job! You’ve now removed category slugs from your product category URLs as well. Your website’s URLs are now cleaner and more professional.

Best Practices and Considerations

As you’ve learned how to remove category slugs from your WordPress URLs, it’s important to keep in mind some best practices and considerations to ensure a smooth transition. Here are a few things to consider:

Use a child theme for modifications:

To maintain the integrity of your parent theme and prevent any conflicts or loss of changes during updates, it’s highly recommended to create and use a child theme for making modifications.

This way, any customizations you make, including removing category slugs, will be kept separate from the parent theme. In the event of a parent theme update, your modifications will remain intact.

Back up your functions.php file:

Before making any changes to the functions.php file, it’s a good practice to create a backup. This allows you to revert back to the original file in case something goes wrong during the modification process.

To create a backup, simply make a copy of the original functions.php file and store it in a safe location on your computer.

Thoroughly test your website:

After making any modifications to your functions.php file and permalinks structure, it’s crucial to thoroughly test your website.

Visit different category and product category pages, click on various links, and ensure everything is functioning correctly. Look out for any unexpected issues or broken links.

Stay up-to-date with WordPress updates:

WordPress frequently releases updates that may include changes to the core functionality or structure. When updating your WordPress installation, ensure that the modifications you made to remove category slugs remain intact.

It’s always a good idea to recheck your website’s URLs after each update to ensure they are still displaying correctly.

Consider potential impacts on SEO:

While removing category slugs can have potential SEO benefits, it’s important to be aware of any potential impacts. If your website has an established presence on search engines and you’ve already built external links to your existing URLs, changing the URL structure could impact your SEO rankings.

Consider implementing proper 301 redirects from the old URLs to the new ones to maintain SEO equity.

Communicate changes to search engines:

To assist search engines in understanding the changes you’ve made to your URLs, it’s recommended to use the Google Search Console or other search engine tools to submit an updated sitemap. This helps search engines discover the new URL structure and index your website accordingly.

Monitor for any broken inbound links:

After making changes to your URLs, keep an eye on any inbound links pointing to your website from external sources. If those links are now broken due to the modified URL structure, consider reaching out to the website owners or administrators to update the links.

By following these best practices and considering the potential impacts, you can ensure a smooth transition while removing category slugs from your WordPress URLs. Enjoy the benefits of cleaner URLs and improved user experience!

Conclusion

Congratulations! You’ve successfully learned how to remove category slugs from your WordPress URLs without relying on plugins. By implementing this simple modification, you can enjoy cleaner, more user-friendly URLs and potentially improve your website’s SEO.

In this blog post, we covered the following key points:

  1. Understanding Category Slugs in WordPress URLs: We explored the concept of category slugs in WordPress URLs and discussed why removing them can be beneficial for your website.
  2. Benefits of Removing Category Slugs: We explored the advantages of cleaner URLs, improved keyword prominence, higher click-through rates, enhanced user experience, and simplified sharing and link building.
  3. Removing Category Slugs for Blog Post Categories: We provided a step-by-step guide to removing category slugs from blog post category URLs using code snippets in the functions.php file.
  4. Removing Category Slugs for Product Categories: We followed up with a similar step-by-step process for removing category slugs from product category URLs.
  5. Best Practices and Considerations: We highlighted important considerations, such as using a child theme for modifications, thoroughly testing your website, staying up-to-date with WordPress updates, considering SEO impacts, and monitoring for broken inbound links.

Remember, while removing category slugs can benefit your website, always ensure you have a backup of your files, test thoroughly, and consider the potential impacts on SEO and external links.

By following these guidelines and maintaining best practices, you’re well on your way to creating a cleaner and more user-friendly website. Enjoy the benefits of improved URLs and a better user experience for your visitors.

If you have any questions or need further assistance, don’t hesitate to reach out. Happy WordPress customization!

40 Simple Hacks For Creating Content People Love To Read
Protected by Google for safe sign-ups