1

To all would-be close-voters: This question is not a duplicate, because I need to remove all scripts added by plugin using remove_action.

In Very stubborn wp_register_script / add_action vs remove used wp_dequeue_script function to remove scripts one by one, but I need to remove them all (which was added by plugin using add_action) using remove_action!

wp_dequeue_script function can't remove custom code like this:

add_action('wp_head', 'print_style');

function print_style() {
    echo '<style>body { margin: 0; }</style>';
}

I have installed WP Polls plugin. I am using it in some of the pages and need to deregister its styles and scripts on other pages.

I found some code in the plugin which registers scripts:

Function: Enqueue Polls JavaScripts/CSS

add_action('wp_enqueue_scripts', 'poll_scripts');
function poll_scripts() {
    // code
}

So for deregistering its scripts i am using next code in my functions.php:

add_action( 'wp_enqueue_scripts', 'deregister_polls_scripts_and_styles' );

function deregister_polls_scripts_and_styles() {
    if ( is_home() ) {
        remove_action( 'wp_enqueue_scripts', 'poll_scripts');
    }
}

So it must deregister polls scripts on homepage, but id does not. What i am doing wrong?

P.S. 1

Scripts are removing in this case:

add_action( 'after_setup_theme', 'wc_deregister_polls_scripts_and_styles' );

function wc_deregister_polls_scripts_and_styles() {
    remove_action('wp_enqueue_scripts', 'poll_scripts');
    remove_action('wp_head', 'poll_head_scripts');
}

But i need to remove it on some pages like this:

add_action( 'after_setup_theme', 'wc_deregister_polls_scripts_and_styles' );

function wc_deregister_polls_scripts_and_styles() {
    if ( is_page('developers') ) {
        remove_action('wp_enqueue_scripts', 'poll_scripts');
        remove_action('wp_head', 'poll_head_scripts');
    }
}

So in this case it does not work.

I have installed another plugin and all works fine in this case. Same actions, BUT all works:

add_action( 'wp_enqueue_scripts', 'wc_deregister_scripts_and_styles' );

function wc_deregister_scripts_and_styles() {
    if ( !is_home() && !is_tax('brand') ) {
        remove_action( 'wp_enqueue_scripts', 'uag_frontend_styles', 999 );
        remove_action( 'wp_footer', 'uag_footer_scripts');
    }
}

P. S. 2

This is another plugin.

add_action('wp_enqueue_scripts', 'uag_frontend_styles', 999);
function uag_frontend_styles() {

    /* main stylesheet */
    wp_register_style( 'uag_style', uag_url . 'css/uag.css');
    wp_enqueue_style('uag_style');
    }

This is Polls:

add_action('wp_enqueue_scripts', 'poll_scripts');
function poll_scripts() {
    global $text_direction;
    if(@file_exists(get_stylesheet_directory().'/polls-css.css')) {
        wp_enqueue_style('wp-polls', get_stylesheet_directory_uri().'/polls-css.css', false, '2.63', 'all');
    } else {
        wp_enqueue_style('wp-polls', plugins_url('wp-polls/polls-css.css'), false, '2.63', 'all');
    }
    if('rtl' == $text_direction) {
        if(@file_exists(get_stylesheet_directory().'/polls-css-rtl.css')) {
            wp_enqueue_style('wp-polls-rtl', get_stylesheet_directory_uri().'/polls-css-rtl.css', false, '2.63', 'all');
        } else {
            wp_enqueue_style('wp-polls-rtl', plugins_url('wp-polls/polls-css-rtl.css'), false, '2.63', 'all');
        }
    }
    $poll_ajax_style = get_option('poll_ajax_style');
    $pollbar = get_option('poll_bar');
    wp_enqueue_script('wp-polls', plugins_url('wp-polls/polls-js.js'), array('jquery'), '2.63', true);
// Other same code
}
2
  • I have installed another plugin and used same solution and all works...
    – Alexander
    Commented Oct 3, 2013 at 6:48
  • can you check if you are using a static home page in that case you need to use the conditional tag is_front_page() instead of is_home(). If everything is fine with that, can you please share the code written in plugin to enqueue the js file. Commented Oct 3, 2013 at 7:16

2 Answers 2

3

An action can be removed in time that pass since it is added to it is fired. So after is added, before is fired.

You say the plugin has thiss code:

add_action('wp_enqueue_scripts', 'poll_scripts');

function poll_scripts() {
    // code
}

But you don't say if this code is inside a function hooked somewhere.

If it is not in any function, the action is added as soon the plugins is loaded, so you (so before theme is loaded) so if you do it from a theme, you can remove this action everywhere before wp_head(), first hook you have available on theme is after_setup_theme,

So this one will work:

add_action('template_redirect', 'remove_poll_scripts');

function remove_poll_scripts() {
  if ( is_home() ) remove_action('wp_enqueue_scripts', 'poll_scripts');
}

if in your functions.php you just write

remove_action('wp_enqueue_scripts', 'poll_scripts');

It will work because functions.php is loaded after the plugin and before 'wp_enqueue_scripts' is fired: so you are perfectly in time but you can't check in which page remove the scripts.

Moreover, if you try to do the exactly same thing from a plugin, and in your plugin file you write

remove_action('wp_enqueue_scripts', 'poll_scripts');

without wrapping it in any hook, it can fail: no one can assure your plugin is not loaded before the plugin that add the script and in that case you try to remove the action before it is added.

6
  • Ok, this is good idea, but in this case i cant check on which page i need to remove scripts. See P.S. 1 in main question.
    – Alexander
    Commented Oct 8, 2013 at 6:07
  • Use template_redirect instead of after_setup_theme. See the edit. @Alexander
    – gmazzap
    Commented Oct 8, 2013 at 9:43
  • But why same solution worked on another plugin?
    – Alexander
    Commented Oct 9, 2013 at 5:47
  • Probably other plugin handle the enqueue differently. @Alexander
    – gmazzap
    Commented Oct 9, 2013 at 11:14
  • See P. S. 2 plz
    – Alexander
    Commented Oct 9, 2013 at 21:22
0

Just a thought here - try moving back the priority on your action. Without the priority, it's going to be defaulting to 10, same as the original registration. Because of the way child themes work, that means you are firing the deregister before the register/enqueue takes place.

add_action( 'wp_enqueue_scripts', 'deregister_polls_scripts_and_styles', 99 );
3
  • So only way to remove them all - use mechanism like this wp_dequeue_script( 'jquery' );? So i need to dequeue every script manually?
    – Alexander
    Commented Oct 3, 2013 at 15:22
  • This thing does not working.
    – Alexander
    Commented Oct 4, 2013 at 11:58
  • All working if i only use remove_action('wp_enqueue_scripts', 'poll_scripts'); in my functions.php without any remove action.
    – Alexander
    Commented Oct 4, 2013 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.