I was trying to figure out how to remove product category slug in WooCommerce (eg: remove /product-category/ ) from my URLs category and I just couldn’t seem to remove it.
For eg: https://tistook.com/product-category/frock
I just wanted to be https://tistook.com/frock
Finally, I got a quick solution to do this task.
It works for me, and it might work for you.
If you find a bug, please let me know on mail: info@tutorialswebsite.com.
How to remove product category slug in woocommerce without plugin.
You have to follow few steps and setup the rules to do this task. The code is experimental and it is better to avoid using it at production sites. This is exactly what you need to remove the slug from the category pages.
Step-1: Change product category base field of the permalinks page
Enter a “.” in the product category base field of the permalinks page. This will help to generate the urls around the site like we want.
Step-2: Be sure you don’t have any page, post with the same name (slug)
Be sure that you don’t have any page, post or attachment with the same name (slug) as the category page or they will collide and the code won’t work.
Step-3: Copy and Paste below code:
You have to copy and paste below code in functions.php file of your current theme folder:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* Code Purpose : Remove woocommerce product-category slug Author: Tutorialswebsite */ 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; }); add_filter('term_link', 'term_link_filter', 10, 3); function term_link_filter( $url, $term, $taxonomy ) { $url=str_replace("/./","/",$url); return $url; } |
Feel free to test the code and let me know if it’s not working on any particular case. I only tried with two levels of category and pagination, nothing else.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Also Read: How to remove the Password Strength Meter from WooCommerce on the checkout page
Conclusion:
Technically we filter every request and check If we have any product category with that name, and if we do, we simply set the vars of the request as if we were looking at the category list. It always happens before executing the main sql queries. so this shouldn’t be a waste of resources. It works with page cache plugins and I think that if you use a persistent cache, the code could be modified to make the most of it.
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co