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.

To reduce http requests, I'm combining all scripts that my theme uses into a single minified .js file each time theme options is saved.

One of the files in this .js is the jQuery library.

Rather than including jQuery as a separate file (using wp_enqueue_scripts), I'm including the wordpress core jquery file as part of the output stream of this master .js file

The problem is that since I'm not using wp_enqueue_scripts() to load jQuery, there's is a possibility it can be included more than once on the page via a plugin that does not know its already there.

Can I set a flag so that WordPress knows that file already exists on the page?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You really shouldn't do it, it's not a good practice. You should always use the jQuery version supplied with WordPress package. If you use your own jQuery version in your own file you would have to update it manually every time the jQuery gets updated in WordPress package.

If you really insist on doing that, the most simple, hackish solution I can think of is to first deregister the original jQuery, then register your file (minified.js) as jQuery. You will surely run into problems of incompatibility with plugins sooner or later.

share|improve this answer
    
I think the OP is actually intending to use the core-bundled version of jQuery, via wp_enqueue_script( 'jquery' ); he just wants to enqueue it in a custom way that would allow the Theme to minify its scripts. –  Chip Bennett May 8 '12 at 15:15
    
@ChipBennett Not really, read the third paragraph of his question. :) –  Mateusz Hajdziony May 8 '12 at 15:19
    
Sorry, let me re-phrase that. :) I think the OP is intended to use the core-bundled version of jQuery, but wants to enqueue it in a custom manner. In any case, I think the premise is misguided. –  Chip Bennett May 8 '12 at 15:31
    
@Chip Bennett: Yes, that's correct. I'm using the core-bundled version but rather than include it as a standalone file, I'm combining it with the other scripts that my theme relies on (slider.js, webfonts.js, etc) into a single, minified .js file. –  Scott B May 8 '12 at 15:38
add comment

Themes should not concern themselves with code minification. That's Plugin territory. Themes are presentational, not functional.

By trying to add JS minification into the Theme, you're actually preventing a caching Plugin from performing this function, and potentially introducing breakage (such as duplicate loading of the jQuery library).

share|improve this answer
    
Several themes (mine included) feature a slider widget that requires jQuery to operate. In your opinion, should sliders that require js always be separated into a plugin? –  Scott B May 8 '12 at 16:13
    
I don't see it that way. The Theme requires jQuery to be enqueued, so it properly enqueues jQuery. That's all clearly on the presentational side of the line. JS minification is just as clearly on the functional side of that line. And non-standard means of enqueuing script libraries, in addition to being functional, is also a practice to be discouraged by Themes and Plugins. –  Chip Bennett May 8 '12 at 16:17
    
Ah, I understand your point better then. You aren't against the theme including jQuery, just against including it in a "non standard" way (combining template scripts into a single minified file) should be left to a plugin because its functional. I can live with that as a guiding principle, but I believe there should be a way to embrace best practices in web design/dev (minimizing http requests, minifying scripts) without requiring a plugin. –  Scott B May 8 '12 at 16:38
    
Ideally, if I could load all of my scripts into a single wp_enqueue_scripts call and pass a parameter so that the end result would be a single .js consisting of all of the files, that's what I'm after. I'm fine leaving the minification to 3rd party optimizers. –  Scott B May 8 '12 at 16:48
    
"there should be a way to embrace best practices in web design/dev (minimizing http requests, minifying scripts) without requiring a plugin" - What's wrong with a Plugin? Both Themes and Plugins use the same Plugins API to do the same things. In this case, best practice is separating presentation from function, so putting minification in a Plugin is the best-practice approach. –  Chip Bennett May 8 '12 at 17:18
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.