Remove taxonomy slugs (categories, post tags and custom taxonomies) from URL

In this post I will show you how to remove «category» from WordPress categories URL, how to remove «tag» from post tags URLs, and how to remove custom taxonomy slugs («product_cat») from term URLs: Here is the main part of thе code, you can insert it into your current theme functions.php, just do not forget […]

#permalinks, #taxonomies  /   46

In this post I will show you how to remove «category» from WordPress categories URL,

How to remove category slug from URLs

how to remove «tag» from post tags URLs,

How to remove tag from post_tags URLs.

and how to remove custom taxonomy slugs («product_cat») from term URLs:

Remove taxonomy slug from term URL.

Here is the main part of thе code, you can insert it into your current theme functions.php, just do not forget to change taxonomy names/slugs in each function to your own values.

add_filter('request', 'rudr_change_term_request', 1, 1 );
 
function rudr_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( 'term_link', 'rudr_term_permalink', 10, 3 );
 
function rudr_term_permalink( $url, $term, $taxonomy ){
 
	$taxonomy_name = 'product_cat'; // your taxonomy name here
	$taxonomy_slug = 'product_cat'; // 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
	if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url;
 
	$url = str_replace('/' . $taxonomy_slug, '', $url);
 
	return $url;
}

And do not forget about 301 redirect from old URLs, it is required for your website SEO.

add_action('template_redirect', 'rudr_old_term_redirect');
 
function rudr_old_term_redirect() {
 
	$taxonomy_name = 'product_cat';
	$taxonomy_slug = 'product_cat';
 
	// 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;
 
}

The code was tested with different hierarchical and non-hierarchical taxonomies and works great with this permalink settings.

Recomended permalink settings for the code.

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 46

← Older
  • Works for me, thanks from Lima – Perú.

  • Hi Misha, maybe it’s not the right place to write it, but is there a way to modify the permalink of tassonomy pages by entering the top term with children?
    For example:
    https://www.sitename.com/taxonomy_name/top_term/child_term/

  • I love you. This worked perfectly.

  • Hi,

    This ‘works’ but I’m getting this error in the error logs

    [proxy_fcgi:error] [pid 5193] [client 127.0.0.1:58354] AH01071: Got error ‘PHP message: PHP Notice: Undefined index: attachment in ….

    which is in this line

    // Request for child terms differs, we should make an additional check
    	if( $query['attachment'] ) :  // <--- ERROR HERE
    		$include_children = true;
    		$name             = $query['attachment'];
    	else:
    		$include_children = false;
    		$name             = $query['name'];
    	endif;

    Any ideas why?

  • Hi Misha,
    My code error: $name = $query[‘name’];

  • Thanks for this amazing article; Any idea on how to remove custom post type slug from url?

  • Hey,
    script seems to be nice. But in my case its not working with the grand-child terms /cat1/cat1b/term.

    The URL rewrite works but if i try to enter the archive i get a 404.

    Can you help me ?

  • Hi Misha,

    I have a custom taxonomy “dealstore”. And I’ve followed this guide, at the end; in WordPress admin I do see the stores without /dealstore/ when I hover my mouse above “view”.. but when I click on it (view) it gives me a 404 error. And the domain.com/dealstore/store still works.

    Mind helping me with this?

  • Hi, in my case I want to remove language slug like en_US (and more languages) from URL because they create som conflicts with some plugin functionnalities. I tryed your code but nothing happens.

    https://www.mydomain.com/installation-folder/en_US (and fr_FR…)/…

    Any advice?

    Thank you very much

  • Hi, Misha

    I got a problem when I was doing this. My url structure is like “http://domain.com/tax_name/term/child_term/child_term/”. I was trying to remove “tax_name” from url, It worked ok when I access the second level child terms, but when it’s “/term/” or “/term/child_term/”, the page showed with template “archive.php” instead of “taxonomy.php”.

    Could you help me? Thank you very much.

    • Hi,

      If you use child terms, the code will be different. Unfortunately I do not have the ready code to share it with you.

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email