Last active
April 19, 2017 13:59
-
-
Save ibes/042b3c16cadfd30683778a269eda3e4c to your computer and use it in GitHub Desktop.
If you want to have cleaner URLs and want to delete the taxonomy part of the url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add to functions.php | |
// thanks to: https://rudrastyh.com/wordpress/remove-taxonomy-slug-from-urls.html | |
add_filter('request', 'rudr_change_term_request', 1, 1 ); | |
function rudr_change_term_request($query){ | |
$tax_names = array('manufacturer', '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; | |
// if find product with name | |
$posts = false; | |
if (isset($query['attachment'])) { | |
$posts = get_posts(array( | |
'name' => $query['attachment'], | |
'posts_per_page' => 1, | |
'post_type' => 'product', | |
'post_status' => 'publish' | |
)); | |
} | |
if ( isset($posts) && !is_wp_error($posts) && isset($posts[0]) ) { | |
$query['page'] = ''; | |
$query['product'] = $name; | |
$query['post_type'] = 'product'; | |
$query['name']= $name; | |
unset($query['attachment']); | |
return $query; | |
} | |
foreach ($tax_names as $tax_name) { | |
$term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists | |
if ( $term && !is_wp_error($term) ) { break; }// term found so don't search on} | |
} | |
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( 'term_link', 'rudr_term_permalink', 10, 3 ); | |
function rudr_term_permalink( $url, $term, $taxonomy ){ | |
$taxonomies = array(); | |
$taxonomies[] = array('tax_name' => 'manufacturer', 'tax_slug' => 'manufacturer'); | |
$taxonomies[] = array('tax_name' => 'product_cat', 'tax_slug' => 'product_cat'); | |
// your taxonomy name here | |
// the taxonomy slug can be different with the taxonomy name (like 'post_tag' and 'tag' ) | |
// exit the function if taxonomy slug is not in URL | |
foreach( $taxonomies as $tax ) { | |
if ( strpos($url, $tax['tax_slug']) === FALSE || $taxonomy != $tax['tax_name'] ) continue; | |
$url = str_replace('/' . $tax['tax_slug'], '', $url); | |
} | |
return $url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment