Proper way to use apply_filters() with class functions?

It depends on you where you want to call apply_filters().

It can be right when you defined the $title:

$title = apply_filters( 'my_hook', the_title_attribute( 'echo=0' ) ); // set echo to false
return $title;

Or in the return call:

$title = the_title_attribute( 'echo=0' ); // set echo to false
return apply_filters( 'my_hook', $title );

Then one can hook to the filter like so:

add_filter( 'my_hook', 'some_function' );

PS: As @birgire pointed, the_title_attribute() will echo output, unless the echo argument is set to false. So be sure to use the function with the proper parameters.

Leave a Comment