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'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?

share|improve this question
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
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
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
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.