Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.