Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namncn/6c92491ef27d86879e3d50eb94624b20 to your computer and use it in GitHub Desktop.
Save namncn/6c92491ef27d86879e3d50eb94624b20 to your computer and use it in GitHub Desktop.
<?php
/**
* Change Term Request.
*/
function pixelplus_change_term_request( $query ) {
$tax_name = 'product_cat'; // specify you taxonomy name here, it can be also 'category' or 'post_tag'
// Request for child terms differs, we should make an additional check
if ( $query['attachment'] ) :
$include_children = true;
$name = $query['attachment'];
else:
$include_children = false;
$name = $query['name'];
endif;
$term = get_term_by( 'slug', $name, $tax_name ); // get the current term to make sure it exists
if ( isset( $name ) && $term && !is_wp_error( $term ) ): // check it here
if( $include_children ) {
unset($query['attachment']);
$parent = $term->parent;
while( $parent ) {
$parent_term = get_term( $parent, $tax_name);
$name = $parent_term->slug . '/' . $name;
$parent = $parent_term->parent;
}
} else {
unset($query['name']);
}
switch ( $tax_name ):
case 'category':{
$query['category_name'] = $name; // for categories
break;
}
case 'post_tag':{
$query['tag'] = $name; // for post tags
break;
}
default:{
$query[$tax_name] = $name; // for another taxonomies
break;
}
endswitch;
endif;
return $query;
}
add_filter( 'request', 'pixelplus_change_term_request', 1, 1 );
/**
* Change Term Permalink.
*/
function pixelplus_term_permalink( $url, $term, $taxonomy ) {
$taxonomy_name = 'product_cat'; // your taxonomy name here
$taxonomy_slug = 'product-category'; // Cái này là đường dẫn bạn setting trong Permalink, ví dụ "danh-muc" (đối với Woocommerce mặc định không cài đặt gì thì nó là "product-category")
// exit the function if taxonomy slug is not in URL
if ( FALSE === strpos( $url, $taxonomy_slug ) || $taxonomy != $taxonomy_name ) {
return $url;
}
$url = str_replace( '/' . $taxonomy_slug, '', $url );
return $url;
}
add_filter( 'term_link', 'pixelplus_term_permalink', 10, 3 );
/**
* Redirect Old Term.
*/
function pixelplus_old_term_redirect() {
$taxonomy_name = 'product_cat';
$taxonomy_slug = 'product-category'; // Cái này là đường dẫn bạn setting trong Permalink, ví dụ "danh-muc" (đối với Woocommerce mặc định không cài đặt gì thì nó là "product-category")
// exit the redirect function if taxonomy slug is not in URL
if ( FALSE === strpos( $_SERVER['REQUEST_URI'], $taxonomy_slug ) ) {
return;
}
if ( ( is_category() && 'category' == $taxonomy_name ) || ( is_tag() && 'post_tag' == $taxonomy_name ) || is_tax( $taxonomy_name ) ) :
wp_redirect( site_url( str_replace( $taxonomy_slug, '', $_SERVER['REQUEST_URI'] ) ), 301 );
exit();
endif;
}
add_action( 'template_redirect', 'pixelplus_old_term_redirect' );
@Dexter407
Copy link

Cám ơn bạn đã chia sẻ

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