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 had a quick question regarding how you are supposed to load in scripts with Wordpress. I am developing a theme and I know that the best way to do this is to use the "wp_register_script/wp_register_style" methods and then to enqueue them with "wp_enqueue_script/wp_enqueue_style". Lastly, you add them to the action "wp_enqueue_scripts".

My question is because I'm developing a theme, I have several javascript files I'm enqueueing. Is this a good practice to separate javascript files and enqueue each of them singlely, or should I combine all my javascript files into one file and enqueue just that one file? Ideally, I'd like to be able to keep the files separated for ease-of-future updating, but does this slow Wordpress down when serving your site if it has to enqueue all of those files every time? Does everytime Wordpress serves a page/post, does it have to run through these files, or after it runs once does it cache the files in some way?

I guess I'd like a little more insight on how this action works. Just trying to streamline things best I can. Thanks in advance for the input guys!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

I would't be affraid of wp_enqueue function itself, not PHP slowdown. But enqueuing more javascript files really matters. But not from the PHP point of view. Each website (even in pure HTML) is getting slower with each other request till it brings all the files. The firs request is HTML file (that's why we use gzip compression for this file), next requests are for CSS and JavaScript files, images, flash components and other stuff.

For CSS and JavaScript we have got a minifiers for JavaScript and CSS and that's why we use CSS sprites for multiple images being in just one request. Each request is a slowdown.

I would recomment you a project yslow - to get more insight.

And now back to your question. Merge your JavaScript and minimize it. But keep developer version (mysctipt.dev.js) for future developing and enqueue myscript.min.js to your theme (as WordPress itself does). And ship both of them along with your theme.

And another important thing. If you'll go round wp_enqueue_script hook with your css, some plugins for automatic minifiing of scripts and CSS wouldn't work as expected for users of your theme. Use that function.

share|improve this answer
    
Thanks @david.binda. Definitely helpful to hear that. I had been toying around with the idea of combining & minifying. Sometimes it's the simple answers that will make a world of difference. :) Cheers! –  kaffolder Feb 3 '13 at 16:30
add comment

Well, if you want to argue efficiency, then a pure hard-coded HTML site is about as efficient as it gets, but maintenance is a problem, duplication of effort is a problem, and you don't get nifty dynamic content. So you really end up with a calculation that isn't all about efficiency. You have to consider maintenance and functionality as well, as well as things like server environment.

As far as enqueueing scripts vs hard-coding them into the <head> you aren't going to notice much difference. I haven't measured it but I'd bet you are talking about a difference out five or six decimal places-- 0.0000n or so. That is a price worth paying.

The real lag is going to be transfer time between your server and the browser which you could ameliorate by manually combining your scripts, but then you have a maintenance issue. It bugs me that WordPress doesn't (offer an option to) combine scripts for the front end like it does for the back but there are plugins that will do that.

Long story short, register and/or enqueue your scripts. Overall, you come out better.

share|improve this answer
    
Thanks @s_ha_dum! Very helpful. I would agree...I don't want to lose the ability to easily maintain things. That's why I switched to developing on top of CMS' in the first place! :) I also read on a forum to wrap my front-end enqueue functions in if(!is_admin) so that they're not loaded in on the backend. Every little thing helps! Thanks for your answer! –  kaffolder Feb 3 '13 at 16:28
2  
@kaffolder - if you're enqueueing scripts in the wp_enqueue_scripts action, is_admin is unnecessary because wp_enqueue_scripts only runs on the front end. there's an admin_enqueue_scripts action for enqueueing admin-side scripts. –  Milo Feb 3 '13 at 16:43
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.