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?

  • 1
    Why would you want to unenqueue them? If you do that, then things like comments and such won't work properly. – Otto Feb 21 '11 at 20:31
  • These are my own custom scripts I'd like to unenqueue -- and, incidentally, on pages without comments. If I'm missing something important, I'd rather start with a blank slate and add the one or two I need rather than have to explicitly unenqueue a half dozen or so. – editor Feb 21 '11 at 20:39
  • I've thought of playing with the $wp_scripts global but I'm worried I'm going to unenqueue scripts on more than just the current page. – editor Feb 21 '11 at 20:42

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.

  • 2
    But be aware of your php version before using is_a(): php.net/manual/en/function.is-a.php - look at the notes for version and deprecated – kaiser Feb 22 '11 at 0:06
  • 1
    WordPress itself uses is_a() for this, which is why I used it. – Otto Feb 22 '11 at 7:27
  • 1
    @kaiser: is_a() is used because instanceof was introduced in PHP 5, and WordPress currently still supports instanceof. And it appears is_a() is no longer deprecated in 5.3 - what do you call a "no-longer-deprecated" function? – Jan Fabry Feb 22 '11 at 8:38
  • 1
    i know about the usage. I haven't known about is_a() before i found it used in wp core. @Jan: what do you call a "no-longer-deprecated" function? ... honestly i don't know. I just said/wrote that he should take a look at his php version before usage. Did i say something wrong? Sry, pretty tired at the moment. Long day + night. – kaiser Feb 22 '11 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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