admin管理员组文章数量:1429061
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
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
Share Improve this question edited May 14, 2019 at 17:11 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked May 14, 2019 at 16:47 user145078user1450781 Answer
Reset to default 5It 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.
本文标签: plugin developmentProper way to use applyfilters() with class functions
版权声明:本文标题:plugin development - Proper way to use apply_filters() with class functions? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745493820a2660722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论