0

In my error log I m having this error:

PHP Warning: strip_tags() expects parameter 1 to be string

Which seems to make crash my server . . .

I'm not much aware of Server Conf / Back end Dev, but this are the piece of code being impacted:

<?php
            # show categories 
            $categories = get_the_term_list( $post->ID, 'ev_categories', '<p>', ', ', '</p>' ); 
            $categories = strip_tags( $categories );
            echo $categories;
            if ($categories <> '') echo '<div class="clear"></div>';
        ?>

?php

# show categories 
$categories = get_the_term_list( $post->ID, 'ev_categories', '<p>', ', ', '</p>' ); 
$categories = strip_tags( $categories );

if ( strpos($categories,'arts and culture') !== false ) { $catID = 1; };
if ( strpos($categories,'business')         !== false ) { $catID = 2; };
if ( strpos($categories,'community')        !== false ) { $catID = 3; };
if ( strpos($categories,'education')        !== false ) { $catID = 4; };
if ( strpos($categories,'sport')            !== false ) { $catID = 5; };

?>

<?php

# show categories 
$categories = get_the_term_list( $post->ID, 'ev_categories', '<p>', ', ', '</p>' ); 
$categories = strip_tags( $categories );

if ( strpos($categories,'arts and culture') !== false ) { $catID = 1; };
if ( strpos($categories,'business')         !== false ) { $catID = 2; };
if ( strpos($categories,'community')        !== false ) { $catID = 3; };
if ( strpos($categories,'education')        !== false ) { $catID = 4; };
if ( strpos($categories,'sport')            !== false ) { $catID = 5; };

# show locations                    
$locations = get_the_term_list( $post->ID, 'ev_locations', '<p>', ', ', '</p>' ); 
$locations  = strip_tags( $locations  );
#echo $locations ;

?>

Each time, related to the second line below:

$categories = get_the_term_list( $post->ID, 'ev_categories', '<p>', ', ', '</p>' ); 
$categories = strip_tags( $categories );

If anybody is aware how I can fix this, it will be lovely,

  • Are you sure this is string? Maybe get_the_term_list returns false? – Sebastian Kaczmarek Oct 28 '16 at 12:06
  • yes, this is the error inmy error log and the line it refers too :( What do you mean by get_the_term_list ? Thank you :) – tibewww Oct 28 '16 at 12:09
  • According to WP Codex get_the_term_list returns false if the post does not have any associated terms. In this case, when you assign it as a value to $categories it will be boolean false, not a string. Make sure that get_the_term_list returns string – Sebastian Kaczmarek Oct 28 '16 at 12:14
  • Also try using get_terms() instead of get_the_term_list(): developer.wordpress.org/reference/functions/get_terms – Sebastian Kaczmarek Oct 28 '16 at 12:17
  • There's no error handling! – Tom J Nowell Oct 28 '16 at 13:54
2

If you look at the source of get_the_term_list, you will see that it will try to get the terms first. If that doesn't succeed, it will return an error or false. So you would have to account for that before you try to strip the tags:

if (!is_wp_error ($categories) && false != $categories)
  $categories = strip_tags( $categories );

In all other cases get_the_term_list returns a string and your code should work fine.

By the way, it's not very useful to have get_the_term_list to add <p> tags if you strip those away immediately. Also, there is a filter towards the end of get_the_term_list that you could use to strip the link tags that are added by the function. Finally, you may want to take a look at get_the_terms, which will give you a nice array of terms without any tags to strip.

| improve this answer | |
  • Im still having those error in the error log :(, I tried to change get_the_term_list by get_terms(), also to add your code suggested, but it s not changing anything unfortunatly. Do you know any other way by any chance ? Thanks a lot for your time. – tibewww Oct 28 '16 at 15:22
  • Try var_dump on $categories to see what it looks like. That should help you track the bug down. – cjbj Oct 28 '16 at 16:39
0

??? $categories is an array, and it cant be parsed as string. instead, you'd better to do this:

foreach($categories as $name =>$value){
   $locations[] = strip_tags($value->slug);
}

or another way for arrays

array_map('strip_tags', $categories);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.