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.
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.
What am I doing wrong?
CodePudding user response:
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;
}
}
CodePudding user response:
$cats = explode($separator,$thelist);
The source value $separator
is not allowed to be an empty string (or equivilant), therefore you can either check the value before you run the explode
or you can use a ternary operator within the function:
A
Ensure the given $seperator
value is not empty;
if ( !empty($separator) && !empty($thelist) ) {
$cats = explode($separator, $thelist);
}
Example using A here:
$seperator = "";
$thelist = "1 2 3 4 5 6 7 8 9";
$cats = "failed";
if ( !empty($separator) && !empty($thelist) ) {
$cats = explode($separator, $thelist);
}
print_r($cats);
// output string "failed"
B
Set a default seperator value of your choice if it is empty;
if (!empty($thelist) ) {
$cats = explode( ($separator?:' '),$thelist);
}
Example using B here:
$seperator = "";
$thelist = "1 2 3 4 5 6 7 8 9";
$cats = "failed";
if (!empty($thelist) ) {
$cats = explode(($separator?:' '),$thelist);
}
print_r($cats);
// output array of numerical values.
Further to your question:
$catname = trim(strip_tags($cat));
The "Strip_tags" function is extremely problematic and can easily return empty strings if the HTML tags in the string do not line up, such as <p>something here.
this can result in empty strings as you example. I can't say if this is what's happening for you but I would suggest you try and comment out this function and see if that improves the result.
I would suggest you can build yourself a more robust preg_replace()
functionality to remove specific characters such as anything that is not alphanumeric, etc..