admin管理员组文章数量:1391999
The WP_Filters
API has a huge downside. It doesn't support unhooking of namespaced functions/class functions and so on to the best of my knowledge, but I refuse to believe that nor the core team, nor someone else came up with a way to do it.
I've read, searched but to no avail, the only things that are out there don't actually work.
An example of what I'm trying to achieve:
Class Test
{
public function __construct()
{
add_action( 'init', [$this, 'testFunction'] );
}
public function testFunction()
{
echo 'Hey';
}
}
new Test;
remove_action( 'init', ['Test', 'testFunction'] ) // and so on.
So, when we're actually hooking that function, what WP does internally is add_filter
creates a unique hash for an object, in order to identify it by using _wp_filter_build_unique_id/
, the problem with this function or rather PHP, is that it uses spl_object_hash
to build that identity. This function doesn't give you an unique hash through requests, so, if you do remove_action( 'init', 'computedHashForTheObjectAndFunction' )
, it will not work, because on the next request, it's gonna be a new one.
My assumption is that you can remove a function if you have that object's instance, but that's a rare, rare sight.
What can I do?
Edit: Yup, if you have access to the instance of the class in the same request, you can basically re-hash it to the same value and it will be able to remove the hook for you, so, inside my class, if I do remove_action( 'init', [$this, 'testFunction'], 10 );
, it will work. Outside of that, though, is as I've said - impossible without an instance. Re-intializing the object just to get its identity is very, very bad because you might end up with unwanted functionality, so, that's out of the question.
本文标签:
版权声明:本文标题:hooks - Is there no concise way, a library maybe, to help with unhooking class functions and so on? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744705430a2620814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论