0

I used the 'the_content' filter to check if the content of an page is empty and to set an alternative content:

add_filter( 'the_content', 'my_content' );     
function my_content( $content ) {
    // check if we the posttype is correct   -------------------------------
    ...
    if (strlen($content) == 0) {
      return 'new content';
    }
    return $content;
}

When I open an empty post with the correct posttype I will get an empty $content with string length (608). In this case will show and emtpy page and not the "new content" :(

I included the following debug information:

    $temp = trim($content);
    var_dump($temp);

    if (strlen($content) == 0) {
      return 'new content';
    }

and get as an result: string(608) " "

Any Idea what is going on and how to fix it?

4
  • Why don't you try if(empty($content)) condition? Mar 6, 2017 at 13:09
  • Please share full code because I have tried to figure it out but I am getting correct results. Mar 6, 2017 at 13:14
  • thx for pushing me :) the riddle is solved: one plugin "addthis.com" injected some content before my filter but :) addthis just added an html comment <!-- some text --> and this text will not be shown in the browser even if I echo or var_dump it :) so sorry!!! I was only able to see it by iterating over the string... Mar 6, 2017 at 14:23
  • If the riddle is solved then answer yourself below and accept it or close this question. thanks Mar 7, 2017 at 5:22

1 Answer 1

0

It was an presentation issue as the content was injected by another plugin and it was an HTML comment "" and this Text will not be shown by an echo or even an print_r() or var_dump()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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