Skip to content

Instantly share code, notes, and snippets.

@timersys
Last active May 24, 2023 09:01
Show Gist options
  • Save timersys/78a2305d810c27efcb10ce62712d1d95 to your computer and use it in GitHub Desktop.
Save timersys/78a2305d810c27efcb10ce62712d1d95 to your computer and use it in GitHub Desktop.
Remove product-category slug from Woocommerce
<?php
/*
Plugin Name: Remove product-category slug
Plugin URI: https://timersys.com/
Description: Check if url slug matches a woocommerce product category and use it instead
Version: 0.1
Author: Timersys
License: GPLv2 or later
*/
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;
});
@aboixet
Copy link

aboixet commented Nov 21, 2016

Hi!

I try to use this plugin with WPML, and we've an error when I go into sub-category... :(

@ifurlo
Copy link

ifurlo commented Jan 29, 2017

Hi,

thanks to the developer to crate this project/plugin.

By the way, can I suggest to you to add (if is possible) the same filter to left the product-tag-slag as well?

Cheers,
ifurlo

@Johanby
Copy link

Johanby commented Feb 20, 2017

Hi!
Works great but the old url still remains. I guess i need something to redirect all /product-category/ to the new urls. Do you have any ideas on that. I could do it manually for all categories I have right know but the ones that will be added i wish not to do the redirect manually.

@schalipp
Copy link

Hi, this works well for me in our shop, but for one problem:

It breaks on 3rd level product categories, i.e.
/clothes/shirts/longsleeve/ --- will create a "page not found"
/clothes/shirts/ --- is fine, works without problems

I don't really know why, there does not seem to be a level restriction in the code, but as soon as I create 3rd level categories they don't get resolved. So for now I just restrict our shop to 2 category levels. Maybe someone can have a look at the code and find out why this is happening?

Thanks,
Sebastian

@gerharddt
Copy link

gerharddt commented Sep 4, 2017

OK, so I added the additional piece of code from here:
https://wordpress.stackexchange.com/a/258085/87947

This was to fix the problem where it didn't want to work on 3rd level of product categories.

The pagination seems to be broken now, If I click on page 2/3/etc it takes me to a product. Is there something I can do to curb that?

@AponAhmed
Copy link

I want to remove product-tag also

@Delik
Copy link

Delik commented Nov 15, 2017

Dont work for me - ERR_TOO_MANY_REDIRECTS

@rickrocky
Copy link

Hi,
Am new to SEO. I would Like to know how to remove .php from my site.
How to make friendly URL for my site. if u don't bother I will put my site here for the reference.www.britishindiancurryhut.sg.
Can you help me in this?

@riteshtailor2002
Copy link

Hi,

The code is working fine but the product pagination not working and giving 404 found error. Can you please help....

thanks

@cinghaman
Copy link

Got it working as per someone's comment add these two lines

$slug = ! empty( $vars['pagename'] ) ? $vars['pagename'] : ( ! empty( $vars['name'] ) ? $vars['name'] : ( ! empty( $vars['category_name'] ) ? $vars['category_name'] : $vars['attachment'] ) ); $slug_array = explode( '/', $slug ); // New line to be added for subcategory $slug = array_values( array_slice( $slug_array, -1 ) )[0]; // new line to be added for subcategory $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 ) ) );
To make it work for subcategory (just the code above won't work for subcategory)

@cinghaman
Copy link

Does anyone know how we can use Regex
url/product-category/product-slug to url/product-slug so as to add redirects?

@cinghaman
Copy link

@gerharddt were you able to make it work with Pagination? finally, it doesn't work for me or anyone else figured out how to make it work with pagination.

@gerharddt
Copy link

@gerharddt were you able to make it work with Pagination? finally, it doesn't work for me or anyone else figured out how to make it work with pagination.

I ended up just renaming it and the client was happy. Please see the function below:

// the neat way using WooCommerce filter - permalinks
function product_cat_setup($args){
$args['rewrite']['slug'] = 'shop';
return $args;
}
add_filter('woocommerce_taxonomy_args_product_cat', 'product_cat_setup');

@cinghaman
Copy link

@gerharddt ah, unfortunately, won't work for me as I need this product-category gone, but thanks.

@robjstanley
Copy link

For anyone having issues with pagination not working, use this plugin instead https://wordpress.org/plugins/woo-permalink-manager/

@Fullstak-nl
Copy link

Using jet woo builder and/or nested categories?
Use this code:

add_filter('request', function( $vars ) {
	global $wpdb;
	if(isset($vars['jet-woo-builder']))
		return $vars;
	if(isset($vars['error'])){
		$lastVar = basename($_SERVER['REQUEST_URI']);
		$vars['attachment'] = $lastVar;
	}
	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'] ) );
		$_slug = explode('/',$slug);
		$slug = end($_slug);
		$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;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment