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 only want to know what is the purpose of wp_enqueue_script function, if it is used to add javascript and jquery on header then we plugins options to add those files.. but what this function acutally do if I am developing a theme and I have jQuery featured slider on my theme.. do I need this function?

share|improve this question
1  
You will get more information about this function on this codex page codex.wordpress.org/Function_Reference/wp_enqueue_script –  Vinod Dalvi Aug 20 '13 at 7:06
add comment

1 Answer

According to the Codex:

wp_enqueue_scripts() links a script file to the generated page at the right time according to the script dependencies, if the script has not been already included and if all the dependencies have been registered. You could either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script. This is the recommended method of linking JavaScript to a WordPress generated page.

So we can say it's none but a good practice.

As per the WordPress theme reviewers, wp_enqueue_scripts() is recommended, or may be mandatory, because one of the reviewer, reviewed my theme, said:

Themes are required to enqueue all stylesheets and scripts, using wp_enqueue_style()/wp_enqueue_script(), and hooked into an appropriate hook via callback function, rather than hard-coding stylesheet/script links or tags in the template.

But after all, all your confusions related to the enqueue scripting will be vanished after reading:

  1. How to Include JavaScript and CSS in Your WordPress Themes and Plugins
  2. The Ins and Outs of The Enqueue Script For WordPress Themes and Plugins

But quoting the main reasons here for a quick look:

Why Do We Use The Enqueue Function?

...but once you introduce the myriad of other scripts that might potentially run on a WordPress installation, it gets trickier to do what I’ll call “crowd management” with your scripts. So why not load your JavaScript in the Header or the Footer? The answer is pretty simple: By including your JavaScript like that, you run the risk of having conflicts with your JavaScript across your installation (think, multiple plugins trying to load the same scripts, or different versions of that script). Furthermore, your JavaScript will load on every page, even if you don’t need it to. This will cause an unnecessarily longer load time for you pages, and can interfere with other JavaScript, like from a plugin or within the dashboard… and that’s a No No in WP development. By using the wp_enqueue_script function you’ll have better control with how and when you load your JavaScript. You can even decide whether to load into the header or footer.

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