rnevius
rnevius

Reputation: 27092

WordPress remove_filter() to remove theme JS hook via plugin

I have a theme that is adding a custom, full-screen background image via jQuery. The theme is doing this via a class object called td_background. Within the class is a function called wp_head_hook(), and within this hook, a filter is being added for the custom bg. It looks something like this:

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();

I'm now trying to de-register the add_js_hook in a custom plugin I'm writing, but am having trouble wrapping my mind around how to do it with all this nesting. I've tried a few things, such as:

<?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);
?>

I've also tried changing the above to wp_head.

Thoughts? My end goal is to de-register this JavaScript in the footer, so that I can add my own in place of it.

Upvotes: 2

Views: 803

Answers (1)

brasofilo
brasofilo

Reputation: 26065

As it's being instantiated anonymously, we have to use the handy function remove_anonymous_object_filter() from WPSE, it would be something like:

// 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'
    );
}

I tested killing wp_head as I don't have a td_js_buffer_footer_render running.

Upvotes: 1

Related Questions