How to remove only certain WordPress categories from URL

Question

I found this piece of code that works well for stripping the word ‘category’ from all categories in the URL.

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

But is there a way to modify the code so that it removes only certain categories instead of all? For example, I have 3 categories:

  1. site.com/category/play/
  2. site.com/category/sleep/
  3. site.com/category/eat/

I want to have only the word ‘category’ removed from the “play” and “sleep” URLs. In other words, results become:

  1. site.com/play/
  2. site.com/sleep/
  3. site.com/category/eat/

Is it possible to have such outcome in WordPress without using a plugin?

0
, , Rice 5 months 0 Answers 80 views 0

Leave an answer