Contact us 24/7 on:

πŸ’² Services for Individuals and Businesses
πŸ‘‰ Place an Order for Services
πŸ‘‰ Section Navigation

How to remove product-category slug in WooCommerce?

Table of Contents
fhfgghfggh

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: /product-category/frock

I just wanted to be /frock

Finally, I got a quick solution to do this task.

It works for me, and it might work for you.

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.

blank

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:

/*
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.

Picture of xaker
xaker
I promote businesses on the Internet, develop mobile applications and online stores
Share the Post:
5 1 vote
Rating
Subscribe
Notify of
0 comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Our services for businesspeople

Write. Ask questions

We’re always available.
Ask questions – we’ll solve your problem 😎

blank

Place an order for our services

We’ll calculate the cost of your project and propose a strategy :)

blank

Our Contacts

Message us on any of the platforms, we reply instantly

Available every day: 9:00 AM – 6:00 PM (GMT+4)
Send us the order

Email
[email protected]

Don’t be shy, we’re here for you! 😊

Got questions about our prices or web development and marketing services?

Or maybe you’re looking for something unique, tailored just for you? πŸ€΄πŸ‘Έ

Feel free to message our manager, and we’ll take care of your needs!

blank
Shopping cart
Menu πŸ‘ˆπŸ‘ˆπŸΎ
0 items Cart