0

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?

3 Answers 3

2

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.

4
  • 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. May 8, 2012 at 15:15
  • @ChipBennett Not really, read the third paragraph of his question. :) May 8, 2012 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. May 8, 2012 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, 2012 at 15:38
3

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).

5
  • 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, 2012 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. May 8, 2012 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, 2012 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, 2012 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. May 8, 2012 at 17:18
0

The Dependency Minification Plugin does the job nicely for me so I avoid handling minification and concatenation manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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