Search code examples
phpwordpresscategories

How do I fix this "Warning: explode(): Empty delimiter" error message with WordPress categories?


I have the below function in functions.php of the WordPress theme I'm working with. It's supposed to exclude a designated post category (such as "Uncategorized") from being displayed on the front-end of a website. It does work for that purpose.

function the_category_filter($thelist,$separator=' ') {
     // list the IDs of the categories to exclude
     $exclude = array(1);
     // 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);

However, in certain custom post types with different categories than the one that I'm selecting for in the above code, when I'm creating a new post, I get this error

Warning: explode(): Empty delimiter

in the Categories selection panel on the right-hand side of the post editor.

enter image description here

This line is the one it's saying is incorrect:

$cats = explode($separator,$thelist);

I've tried wrapping that part of the code in an if condition to check for the empty delimiter, like this:

 if ( !empty( explode($separator,$thelist) ) ) {     
     $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);
 }

But while that does make the error disappear from the Categories panel, what results is a bunch of blanks where the category names ought to appear.

enter image description here

What am I doing wrong?


Solution

  • With the answers given by Martin and aynber, I solved the problem with this code:

    function the_category_filter($thelist,$separator=' ') {
         // list the IDs of the categories to exclude
         $exclude = array(1);
         // 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   
        if ( !empty( $separator ) && !empty( $thelist ) ) {       
             $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);
        } else {
            return $thelist;
        }
    }