我有一个主题,通过jQuery添加一个自定义的全屏背景图像。这个主题是通过一个名为td_background
的class
对象来实现的。类中有一个名为wp_head_hook()
的函数,在这个钩子中,为自定义bg添加了一个过滤器
class td_background {
// ..some stuff
function __construct() {
add_action('wp_head', array($this, 'wp_head_hook'), 10);
}
function wp_head_hook() {
add_filter( 'td_js_buffer_footer_render', array($this, 'add_js_hook'));
}
function add_js_hook($js) {
// Custom JS added here for background image
return $js
}
}
new td_background();
我现在正试图在我正在编写的自定义插件中注销add_js_hook
,但我很难理解如何使用这些嵌套。我试过一些东西,比如:
<?php
// This...
remove_filter( 'wp_footer', array($td_background, 'td_js_buffer_footer_render'));
// ...and this
remove_filter( 'wp_footer', 'td_js_buffer_footer_render');
// ...and even
remove_filter( 'wp_footer', 'add_js_hook', 100);
?>
我还尝试将上面的内容更改为wp_head
。
想法?我的最终目标是在页脚中注销这个JavaScript,这样我就可以添加我自己的JavaScript来代替它。
由于它是匿名实例化的,我们必须使用WPSE中方便的函数remove_anonymous_object_filter()
,它将类似于:
// Run this from a plugin
add_action( 'template_redirect', 'kill_anonymous_example', 0 );
function kill_anonymous_example() {
remove_anonymous_object_filter(
'wp_head',
'td_background',
'wp_head_hook'
);
}
我测试了杀死wp_head
,因为我没有运行td_js_buffer_footer_render
。