Deregister scripts on unnecessary pages 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.

Read More

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
}

Related posts

2 comments

  1. 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.

  2. 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 );
    

Comments are closed.