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

[SOLVED] Activate plugin button do nothing

1 Answered Questions

Override filter variable not working

1 Answered Questions

3 Answered Questions

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
  • 276 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
  • 7269 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
  • 5605 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
  • 390 View
  • 0 Score
  • 1 Answer
  • Tags:   filters

1 Answered Questions

[SOLVED] What do add_filters() and apply_filter() do?

  • 2011-08-23 12:18:25
  • Michael Watson
  • 12559 View
  • 4 Score
  • 1 Answer
  • Tags:   filters

Sponsored Content