Remove a script from a template file using wp_dequeue_script

I thought I had read it was possible to use wp_enqueue_script() and wp_dequeue_script() directly in individual template files. However, when I add

  wp_dequeue_script( 'myscript' );

to the top of page.php, myscript still gets loaded.

Read More

Is it possible to do what I’m trying to do?

Related posts

Leave a Reply

1 comment

  1. Script dequeuing calls should be added to the wp_print_scripts action hook, like so:

    add_action('wp_print_scripts','example_dequeue_myscript');
    function example_dequeue_myscript() {
       wp_dequeue_script( 'myscript' );
    }
    

    This is because scripts are typically enqueued on the wp_enqueue_script hook, which happens early in the wp_head process. The wp_print_scripts hook happens right before scripts are printed, and thus is latest in the process.