Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress WordPress Hooks - Actions and Filters Filter Functions in WordPress The apply_filters Function

Yohan Park
Yohan Park
7,148 Points

Why tie apply_filters to a value?

when using apply filter we echo out a value that is equal to apply filters. at 1:25

echo $value = apply_filters( …)

why do that instead of

echo apply_filters()

what's the importance?

Ryan Dainton
Ryan Dainton
17,164 Points

Hi Yohan

My understanding is that you will not always want to echo out the results of your apply_filters() method, and so it's best to save it as a variable which can then be echo'd when required.

For example, looking at the 'wp_title' Hook, the filter is applied, but the result is only echoed to the screen if $display === true.

$title = apply_filters( 'wp_title', $title, $sep, $seplocation );
if ( $display ) {
        echo $title;
    } else {
        return $title;
    }

1 Answer

Yohan Park
Yohan Park
7,148 Points

That makes a lot of sense. Thank you, Ryan.