If you are using WordPress and created a custom taxonomy for custom post type then this article is very helpful for you.
When you create a new custom taxonomy for custom post type or default post type in WordPress then its slug will show in the permalink URL structure. Adding slug to permalink displays the taxonomy slug of the current taxonomy and creates the URLs longer which is hard to remember. Also, it creates an issue to rank articles in Google or any search engine.
Also Read: Remove slug from custom post type in WordPress
How to remove the custom taxonomy slug
Let’s start with an example, Suppose I have created a custom taxonomy named “Course Category” for post type “Course”.
Now we will add a new “Course Category” with the name “Web Development”. When the course category will create in the database then we will try to open the course category page URL in the browser address bar.
It will look like the below link:
https://domain.com/course-category/web-development
Now you can see that adding the taxonomy slug creates URL longer and harder to remember.
I just wanted to be https://domain.com/web-development
So now I will cover all steps to remove the custom taxonomy slug from the URL to make the URL shorter and easy to remember.
It works for me, and it might work for you.
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
Remove slug in taxonomy url
You have to follow a few steps and set up 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 taxonomy slug.
Copy and paste the below code to your current theme functions.php file
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
add_filter('request', 'wptwpm_remove_term_request', 1, 1 ); function wptwpm_remove_term_request($query){ $tax_name = 'course-category'; // specify your taxonomy name here // 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( 'term_link', 'wptwpm_change_term_permalink', 10, 3 ); function wptwpm_change_term_permalink( $url, $term, $taxonomy ){ $taxonomy_name = 'course-category'; // your taxonomy name here $taxonomy_slug = 'course-category'; // the taxonomy slug can be different with the taxonomy name // exit the function if taxonomy slug is not in URL if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url; $url = str_replace('/' . $taxonomy_slug, '', $url); return $url; } add_action('template_redirect', 'wptwpm_old_term_redirect'); function wptwpm_old_term_redirect() { $taxonomy_name = 'course-category'; // your taxonomy name here $taxonomy_slug = 'course-category'; // your taxonomy slug here // exit the redirect function if taxonomy slug is not in URL if( strpos( $_SERVER['REQUEST_URI'], $taxonomy_slug ) === FALSE) return; if( ( is_category() && $taxonomy_name=='category' ) || ( is_tag() && $taxonomy_name=='post_tag' ) || is_tax( $taxonomy_name ) ) : wp_redirect( site_url( str_replace($taxonomy_slug, '', $_SERVER['REQUEST_URI']) ), 301 ); exit(); endif; } |
Note: Please replace custom taxonomy name and slug with your own custom taxonomy name and slug
Update Permalink Setting
When all the above steps are done, Please update the wp-admin permalink link setting.
After this Go to Settings
> Permalinks
and saving the permalink structure to end in /%postname%/
may also be necessary.
That’s it! Feel free to test the code and let me know if it’s not working in any particular case.
Wrapping Words!
Thanks for reading 🙏, I hope you found the Remove custom taxonomy slug in WordPress Without Plugin tutorial helpful for your project. Keep learning!. If you face any problem – I am here to solve your problems.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co