PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 16, 2022

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

 May 16, 2022     categories, php, wordpress     No comments   

Issue

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;
    }
} 


Answered By - user3169905
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing