• I’m using the add_filter function to “the_content” to add specific content to specific pages. Right now I have everything being returned in the function. Basically:

    add_filter('the_content', 'check_page');
    
    function check_page($content) {
    	if(is_page(MYPAGE)) {
    		$content .= my_function();
    	}
    	return $content;
    }

    By doing it this way I have to return all the content in “my_function()” instead of echoing or printing it. Are there any drawbacks to doing the following instead:

    function check_page($content) {
    	return $content;
    	if(is_page(MYPAGE)) {
    		my_function();
    	}
    }

    And echoing/printing the content instead of returning it through the function?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    A filter expects you to return something ($content). Why is this a problem?

    If you want to echo or print it in your function you can try this:

    function check_page( $content ) {
    	if ( is_page( MYPAGE ) ) {
    
    		ob_start();
    		my_function();
    		$function_output = ob_get_contents();
    		ob_end_clean();
    	}
    
    	return $content . $function_output;
    }

    Thread Starter gman243

    (@gman243)

    It’s not a problem. I was just adding a lot of HTML and it was getting old typing:

    $returnContent .= "more html content";
    $returnContent .= "more html content";
    $returnContent .= "more html content";
    $returnContent .= "more html content";
    $returnContent .= "more html content";
    $returnContent .= "more html content";
    $returnContent .= "more html content";
    return $returnContent;

    I just changed it to:

    $returnContent .= '
    all html here
    with line breaks
    and everything
    stored in one
    variable
    ';

    Hopefully that makes more sense. Thanks for your response!

    Moderator keesiemeijer

    (@keesiemeijer)

    No problem 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add to the_content using add_filter and echo’ is closed to new replies.