Remove taxonomy base or term from url

I know there are a lot of posts about this here, but NONE of them have answered my problem.

I am working with a custom built theme that has a custom category type (named messagetypes). I have a category created named media. This theme is setup so that I can add a menu item that when clicked, displays all of the items labeled media, but the URL of the page is then mysite.com/messagetypes/media. I want to remove messateypes from the url and have it only mysite.com/media.

The closest I have gotten is when I add to the custom category functions page, where

register_taxonomy( 'messagetypes', ...

is called with

'rewrite' => array( 'slug' => ''),

When I do this, I get the desired result from this page, but all of my other page links break (using permalinks.)

I have also tried adding to the rewrite array

'with_front' => false

but that didn’t work either.

Any help or insight would be great. I’ve wasted too many hours on this and it’s driving me nuts.

Thanks.

1
1

When I do this, I get the desired
result from this page, but all of my
other page links break (using
permalinks.)

Because, quite simply, WordPress hasn’t got a clue you’re asking for a page. It’s doing what you told it to do;

For all URLs that look like http://example.com/X, look for posts with messagetype’s of X.

That’s why you need the ‘identifier’ in there; so that it can distinguish the difference between requests for pages, archives, categories…

If you really want this functionality, adding verbose rewrite rules at the top of the rewrite map would be the way to go;

add_rewrite_rule( 'media/?$', 'index.php?messagetypes=media', 'top' );

If you’ve got a lot of messagetypes, you might want to hook into the creation and deletion of terms and dynamically update & flush the rewrite rules.

Otherwise, repeat the example for each term in your functions.php, then flush your permalinks (just visit the permalink options page in admin).

One last thing, revert your register_taxonomy call ‘back to normal’ i.e. leave out the rewrite argument, and use the term_link filter to swap in the custom permalink ourselves;

function __custom_messagetypes_link( $link, $term, $taxonomy )
{
    if ( $taxonomy !== 'messagetypes' )
        return $link;

    return str_replace( 'messagetypes/', '', $link );
}
add_filter( 'term_link', '__custom_messagetypes_link', 10, 3 );

Leave a Comment