What's a good way to unenqueue all scripts for a single template page?
-
16-10-2019 - |
Question
I've found that my attachment templates are inheriting the enqueued scripts for single posts. My first instinct was to unenqueue these scripts one-by-one or to do an is_attachment()
check before enqueueing them at all.
It strikes me that a more robust way to do this might be to simply be to remove any and all enqueued scripts for that page type; that way, I don't have to update code in multiple places if I add or remove an enqueued script in the future.
Offhand I can't find a way to do this. What's a good way to unenqueue all scripts for just a single page?
Solution
I haven't tried it, but this might do it:
global $wp_scripts;
if (is_a($wp_scripts, 'WP_Scripts')) {
$wp_scripts->queue = array();
}
Basically just resetting the scripts queue to blank. Should work, I think. You'd want this right at the top of your attachment template, probably.
From an optimization perspective, it would be faster to use the is_attachment() method to not enqueue the ones you want at all instead.