By Latheesh V M Villa


2019-05-14 16:47:06 8 Comments

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

1 comments

@Sally CJ 2019-05-14 16:59:32

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.

@birgire 2019-05-14 17:03:16

ps: the_title_attribute() will echo output, unless the echo argument is set to false.

@Sally CJ 2019-05-14 17:06:57

Thanks @birgire. I've added that to the answer. :)

@Latheesh V M Villa 2019-05-14 17:25:35

Thank you, got it cleared

Related Questions

Sponsored Content

1 Answered Questions

Override filter variable not working

1 Answered Questions

3 Answered Questions

1 Answered Questions

[SOLVED] How to use gettext for specific user role

1 Answered Questions

[SOLVED] add_filter priority problem

  • 2016-06-03 20:33:33
  • kenarsuleyman
  • 353 View
  • 2 Score
  • 1 Answer
  • Tags:   functions filters

2 Answered Questions

[SOLVED] Please help me through this example with a filter to understand how they work

  • 2014-01-12 18:55:36
  • user1796995
  • 263 View
  • 2 Score
  • 2 Answer
  • Tags:   filters

2 Answered Questions

[SOLVED] How to pass a parameter to this add_filter example

  • 2013-03-05 09:56:19
  • Emerson Maningo
  • 7158 View
  • 4 Score
  • 2 Answer
  • Tags:   plugin-development

2 Answered Questions

[SOLVED] Using a filter to modify Genesis wp_nav_menu

  • 2012-07-13 12:51:04
  • shawn
  • 5525 View
  • 0 Score
  • 2 Answer
  • Tags:   filters

1 Answered Questions

[SOLVED] What is the scope and persistence of add_filter() and remove_filter()?

  • 2012-11-02 23:25:23
  • Steve
  • 381 View
  • 0 Score
  • 1 Answer
  • Tags:   filters

Sponsored Content