admin管理员组文章数量:1134574
Here is the code in short that is coming from a plugin:
class Post_Views_Counter_Columns {
public function __construct() {
add_action( 'wp', array( $this, 'admin_bar_maybe_add_style' ) );
}
}
I want to remove this admin_bar_maybe_add_style
function. I am trying with following code in my child theme's functions.php
remove_action( 'wp', array( 'Post_Views_Counter_Columns', 'admin_bar_maybe_add_style' ) );
It's not working. Any suggestions?
Here is the code in short that is coming from a plugin:
class Post_Views_Counter_Columns {
public function __construct() {
add_action( 'wp', array( $this, 'admin_bar_maybe_add_style' ) );
}
}
I want to remove this admin_bar_maybe_add_style
function. I am trying with following code in my child theme's functions.php
remove_action( 'wp', array( 'Post_Views_Counter_Columns', 'admin_bar_maybe_add_style' ) );
It's not working. Any suggestions?
Share Improve this question edited Aug 5, 2021 at 12:10 Morshed Alam Sumon asked Aug 5, 2021 at 11:53 Morshed Alam SumonMorshed Alam Sumon 1571 gold badge2 silver badges7 bronze badges 5 |2 Answers
Reset to default 2I don't understand any of it but it seems to work just fine. Thank you @Buttered_Toast for mentioning the useful thread.
add_action("init", function() {
global $wp_filter;
foreach($wp_filter["wp"][10] as $id => $filter) {
if(is_array($filter["function"]) && count($filter["function"]) == 2 &&
get_class($filter["function"][0]) == "Post_Views_Counter_Columns" &&
$filter["function"][1] == "admin_bar_maybe_add_style") {
remove_action("wp", $id);
}
}
}, 99);
I also wrote my own little funciton, very similar to the one mentioned above. Here it is, it considers all priorities of a given hook or only one if you enter a numeric priority.
You can define your own check function.
function dynamically_remove_hooks($hook, $priorities, $callback_check_function)
{
global $wp_filter;
if(!isset($wp_filter[$hook]))
return;
if(is_numeric($priorities))
$priorities = array((int)$priorities);
elseif(!$priorities && isset($wp_filter[$hook]->callbacks) && is_array($wp_filter[$hook]->callbacks))
$priorities = array_keys($wp_filter[$hook]->callbacks);
foreach($priorities as $priority)
{
if(isset($wp_filter[$hook]->callbacks[$priority]))
{
foreach ($wp_filter[$hook]->callbacks[$priority] as $identifier => $callback) {
if ($callback_check_function( $callback )) {
unset( $wp_filter[$hook]->callbacks[$priority][$identifier] );
}
}
}
}
}
You call it like this, if e.g. you would like to remove all hooks created by the CLASS Post_Views_Counter_Columns for the hook name wp
:
dynamically_remove_hooks('wp', false, function($callback) {
return isset($callback['function'][0]) && get_class($callback['function'][0]) == 'Post_Views_Counter_Columns ';
});
You can define all sorts of callback check functions to have various different logics :)
本文标签: How to remove an action that is added inside a class
版权声明:本文标题:How to remove an action that is added inside a class 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736822554a1954350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Post_Views_Counter_Columns
instantiated? – Jacob Peattie Commented Aug 5, 2021 at 13:06