How to Remove the Product-Category Slug in WordPress WooCommerce

How to Remove the Product-Category Slug in WordPress WooCommerce

This WordPress WooCommerce fix will allow you to remove the product-category slug from the URL in WordPress WooCommerce without using a plugin.

From: https://mywebshop.com/shop/product-category/apparel/black-pants
To: https://mywebshop.com/shop/apparel/black-pants

Remove the product-category slug from the WordPress WooCommerce URL

1. Add this snippet to the functions.php file in your WordPress theme.

add_filter('request', function( $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;

2. Enter a “.” in the “product category base” field on the permalinks settings page (wp-admin/options-permalink.php).

How to Remove the Product-Category Slug in WordPress WooCommerce

Enter a “.”in the “product category base” field.

3. Save to flush the rewrite rules.

WordPress Hosting


    1. Did you remember to re-save your permalinks after you added the snippet?
      *also, try and check your caching plugins and your htaccess to see if there are any conflicts in the rewrites?

  1. Hi Thank you for this trick.

    Is-t possible to remove “Shop” in product permalink ?

    Best regards,

  2. Thank you so much! It is working fine.
    Do you also have the snippet for removing the “product” slug for Woocommerce products and the “product-tag” slug for tags?

  3. it works but when it goes to the 3rd subcategory it is 404 error like only works on /baby/baby-girl-clothing/ then its 404 error on /baby/baby-girl-clothing/sets-baby-girl/

  4. This works great for the most part, and I could adapt it for other custom taxonomies as well. Two-level categories work too (for example: site.com/category/subcategory).

    However, pagination will break if you are in sub categories. Root categories will have working pagination, but sub categories will not. So site.com/category/page/2 will work; site.com/category/subcategory/page/2 will return a 404 error.

    Something must be happening up in the wordpress filter chain, because by the time you hit the ‘request’ filter, the request variables already only state the 404 error.

Leave a Reply

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