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