How to remove all enqueued assets from the active theme?

Here you go. I took this out of my mailoptin plugin. Adapt the code to your use case. add_action(‘wp_enqueue_scripts’, function () use ($wp_get_theme) { global $wp_styles; global $wp_scripts; $wp_get_theme = wp_get_theme(); $child_theme = $wp_get_theme->get_stylesheet(); $parent_theme = $wp_get_theme->get_template(); foreach ($wp_scripts->registered as $key => $value) { $src = $value->src; if (strpos($src, “themes/$child_theme/”) !== false || strpos($src, “themes/$parent_theme/”) … Read more

Correct way to enqueue page specific CSS file

So this way a CSS file would be enqueued in the header.php and then another in that specific page.php. No because: all the things that get enqueued are processed in the header but you’re calling it after the header has already been done. The cat has already gotten out of the bag, the horse has … Read more

What’s a good way to unenqueue all scripts for a single template page?

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() … Read more

Loading Modernizr (or other JavaScript libraries) for use in a plugin

wp_enqueue_script() utilizes the WP_Scripts class, which extends the WP_Dependencies class. If you checkout that class, you will see a bunch of arrays at the top. So you could actually loop through list of queued scripts like so: global $wp_scripts; // If inside a function foreach($wp_scripts->queue as $script) { // Do some tomfoolery here… 😉 } … Read more