Hiding category names on the frontend, but not backend, of WP site

I’m using the following code, placed in the functions.php of my WordPress theme, to hide certain category names from the frontend that are used only to organise posts and populate sliders:

function the_category_filter($thelist,$separator=' ') {
 // list the IDs of the categories to exclude
 $exclude = array(1,32,42,4);
 // create an empty array
 $exclude2 = array();

 // loop through the excluded IDs and get their actual names
 foreach($exclude as $c) {
      // store the names in the second array
      $exclude2[] = get_cat_name($c);
 }

 // get the list of categories for the current post     
 $cats = explode($separator,$thelist);
 // create another empty array      
 $newlist = array();

 foreach($cats as $cat) {
      // remove the tags from each category
      $catname = trim(strip_tags($cat));

      // check against the excluded categories
      if(!in_array($catname,$exclude2))

      // if not in that list, add to the new array
      $newlist[] = $cat;
 }
 // return the new, shortened list
 return implode($separator,$newlist);
}
// add the filter to 'the_category' tag
add_filter('the_category','the_category_filter', 10, 2);

The problem is that this code also hides these same category names in the backend. The tick box is there, but the name of the categories is not. So I would like to modify to this code to allow Admins to see names of the categories hidden on the frontend in the backend. Help appreciated. (p.s., I’m very new to code, so please try to be descriptive, or suggest an edit: much appreciated)

Related posts

Leave a Reply

1 comment

  1. Check if the admin environment is loaded at the beginning of your function, and if so, terminate execution by returning the original list:

    function the_category_filter( $thelist, $separator = '' )
    {
        if( is_admin() )
            return $thelist;
    
        // rest of the code
    }