How to Remove Product Category from URL In WooCommerce

remove product category from url woocommerce without plugin

Are you looking for the best and quickest way to remove product categories from URL WoocCommerce without a plugin?

In this post, I want to show you how you can get rid of the category from the product URL without breaking your site or losing SEO significance.

Most WooCommerce store owners are struggling with this issue and would like to figure out how to remove product categories from URL WooCommerce without plugins and without losing the SEO ranking or ending up with hundreds of 404 page not found errors.

Remove Product Category from URL WooCommerce Without Plugin

First, you need to figure out how the WordPress URLs work.

The URLs in WordPress are technically referred to as permalinks and this is important as you remove product categories from URL WooCommerce without a plugin.

You can access the WordPress permalinks under the settings menu and a child menu or sub-menu – Permalinks.

In my previous tutorial on how to remove Index.php from WordPress URL, I illustrated how permalinks should be set up immediately after installing WordPress.

The tutorial can be a good place to begin understanding how permalinks work in WordPress.

There is not much difference between WooCommerce and WordPress when it comes to permalinks since WooCommerce URLs use the same WordPress permalink structure.

Settings to Remove Product Category from URL WooCommerce Without Plugin

When you look at the settings for the WooCommerce permalinks you can see them as shown in the image below :

remove product category from url woocommerce without plugin - settings
On the front you can see the URL appears as shown in the image below :

remove catgory from product URL

We can change this by updating the permalinks to the shop base option as shown in the image below and the same will be updated in the URL structure in the front. This is the quickest way to remove this slug from the URL of the product.

remove product category from url woocommerce without plugin

You can see the results after you update the permalink structure as shown in the image below :

remove product category from url woocommerce without plugin - results

Code to Remove Product Category from URL WooCommerce Without Plugin

Another option we can use code to remove the URL product category slug and it will work just like the settings option works.

This can be a useful way for developers who want to implement this in their plugin development especially when creating a WooCommerce redirect plugin like the most popular WooCommerce redirect after checkout plugin.

You can the code snippet below to the functions.php of your custom theme or for your WooCommerce child theme and this should do the trick to remove the product category from URL WooCommerce without a plugin:

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;
});

 

Conclusion

In this post, we have outlined the two ways you can employ to remove product categories from URL WooCommerce without a plugin.

Using the settings update is the easiest way and can be a good option for nontechie WooCommerce users.

Similar Articles