How to remove /tag/ from WordPress url ( without a plugin! )

  php, tags, wordpress, wordpress-theming

I need to remove /category/ and /tag/ from the urls of my new WP website.
I already found a great and working snippet to remove the /category/ (which I placed inside the functions.php).

But I found no working solution to remove the tag as well.

What I tried so far (aside from YOAST settings, permalink changes etc etc) is this piece of code:

function twr_remove_tag( $string, $type )  {           
    if ( $type != 'single' && $type == 'category' && ( strpos( $string, 'tag' ) !== false ) ) {              
        $url_without_tag = str_replace( "/tag/", "/", $string );
        return trailingslashit( $url_without_tag );          
    }      
    return $string;  
}     
add_filter( 'user_trailingslashit', 'twr_remove_tag', 100, 2);

This is more or less the same code for removing the /category/ and I think that’s pretty close. Maybe just the $type parameter is wrong? What do I have to use here?

Source: Ask PHP

LEAVE A COMMENT