-
-
Save timersys/78a2305d810c27efcb10ce62712d1d95 to your computer and use it in GitHub Desktop.
<?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; | |
}); |
@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 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');
@gerharddt ah, unfortunately, won't work for me as I need this product-category gone, but thanks.
For anyone having issues with pagination not working, use this plugin instead https://wordpress.org/plugins/woo-permalink-manager/
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;
});
Does anyone know how we can use Regex
url/product-category/product-slug to url/product-slug so as to add redirects?