Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
So is it not possible to use wp_dequeue_script directly in a template file? –  supertrue Sep 12 '12 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 '12 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 '13 at 17:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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