We were working with an agency that requires SEO-friendly URLs for the WooCommerce categories. By default, WooCommerce injects product-category into all their category URLs.

In this article, we will discuss changing the product-category slug and completely removing the product-category slug in WooCommerce.

Table of content

Change the product-category slug

If you would like to change the product-category slug to something different, then you can achieve that by following these simple steps.

  1. Go to the WordPress admin dashboard and navigate Settings > Permalinks.
  2. Locate the Optional > Product category base option
  3. Now you can replace the product-category slug with the desired one.
WooCommerce product-category slug
WooCommerce product-category slug

Permanently Remove product-category slug

From an SEO point of view, the URL-like /product-category/clothing is an undesirable URL structure as compared to simple /clothing. Where clothing is a category. That is why sometimes we are required to remove the product-category slug from the URL.

You can do that by pasting the below snippet to the functions.php file.

add_filter('request', 'wrg_remove_category_slug');
add_filter('term_link', 'wrg_term_link_filter', 10, 3);

function wrg_remove_category_slug($vars) {
    global $wpdb;
    if (!empty($vars['pagename']) || !empty($vars['category_name']) || !empty($vars['name']) || !empty($vars['attachment'])) {
        
        $slug = !empty($vars['pagename']) ? $vars['pagename'] : (!empty($vars['name']) ? $vars['name'] : (!empty($vars['category_name']) ? $vars['category_name'] : $vars['attachment']));
        $exists = $wpdb - > get_var($wpdb - > prepare("SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s", array($slug)));
        
        if ($exists) {
            $old_vars = $vars;
            $vars = array('product_cat' => $slug);
            if (!empty($old_vars['paged']) || !empty($old_vars['page'])) $vars['paged'] = !empty($old_vars['paged']) ? $old_vars['paged'] : $old_vars['page'];
            if (!empty($old_vars['orderby'])) $vars['orderby'] = $old_vars['orderby'];
            if (!empty($old_vars['order'])) $vars['order'] = $old_vars['order'];
        }
    }
    return $vars;
}

function wrg_term_link_filter($url, $term, $taxonomy) {
    $url = str_replace("/product-category/", "/", $url);
    return $url;
}

Need Help?

If you find this hard to implement yourself, you can contact us for help.

We believe this article will help you to remove / change the product-category slug in WooCommerce. And if you enjoyed this article, then please follow us for more interesting and helpful tutorials. You can follow us on Facebook and Twitter.

Pin It on Pinterest

Shares