3

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.

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

1 Answer 1

9

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.

3
  • So is it not possible to use wp_dequeue_script directly in a template file?
    – supertrue
    Sep 12, 2012 at 1:09
  • It's possible, but the effectiveness of it will be determined by how the plugin/theme enqueues the script. The method I give should work in the majority of cases.
    – Otto
    Sep 12, 2012 at 3:04
  • Thanks, works fine for child themes and also for stylesheets. (action: wp_print_styles and function: wp_dequeue_style)
    – Diana
    Aug 31, 2013 at 17:16

Your Answer

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

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