3

I want to allow my user to override a function. It's a plugin. I believe that apply_filters() can be used for this. this can be overridden by add_filter().

I am confused about how to use it. Say if i have a class like this

class Example{
  public function hello(){
   $title = the_title_attribute();
   return $title;
  }
}

What change should I make to allow that $title to be modified? where to place the apply filter? Thanks

6

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.

  • 2
    ps: the_title_attribute() will echo output, unless the echo argument is set to false. – birgire May 14 at 17:03
  • 2
    Thanks @birgire. I've added that to the answer. :) – Sally CJ May 14 at 17:06
  • 1
    Thank you, got it cleared – Latheesh V M Villa May 14 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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