WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently rewriting an HTML template into a Wordpress theme. It has a lot of code dependencies, and I'm wondering if this is the proper, most efficient way to add it. This code is listed in my functions.php file.

//Load Javascript
if (!is_admin()) add_action("wp_enqueue_scripts", "my_scripts");

function my_scripts() {
    wp_register_script('easytabs', get_template_directory_uri() . '/js/jquery.easytabs.min.js', array('jquery'), null, true);
    wp_register_script('respond', get_template_directory_uri() . '/js/respond.min.js', false, null, true);
    wp_register_script('adipoli', get_template_directory_uri() . '/js/jquery.adipoli.min.js', array('jquery'), null, true);
    wp_register_script('fancybox', get_template_directory_uri() . '/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), null, true);
    wp_register_script('isotope', get_template_directory_uri() . '/js/jquery.isotope.min.js', array('jquery'), null, true);
    wp_register_script('gmaps_google', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://maps.google.com/maps/api/js?sensor=false", false, null, true);
    wp_register_script('gmap', get_template_directory_uri() . '/js/jquery.gmap.min.js', array('gmaps_google'), null, true);
    wp_register_script('custom', get_template_directory_uri() . '/js/custom.js', array('gmap'), null, true);

share|improve this question

This is the proper way to load scripts. Whether it's effecient will depend on when you actually need to load the scripts. Currently it will load on every front end page. If this is not necessary, you should put some conditionals and call wp_enqueue_scripts when the file is actually needed.

Also if you are loading a script you don't have to manually queue its dependencies (other scripts it requires to have been loaded to work). For instance wp_enqueue_script('custom'); will automatically load 'gmaps_google' and 'gmap' as well - so you don't need to tell WordPress load them.

If you are simply registering, then immediately enqueuing the scripts you can neaten up the code by providing the script's location (and dependencies etc) in wp_enqueue_scripts and not registering them. However, usually it's best to register the scripts and then enqueue them only when needed (e.g. in a shortcode or widget callback) depending on the context.

Finally, wp_enqueue_scripts only fires on the front-end so the !is_admin check is superfluous.

share|improve this answer
Correct me if I'm wrong, but you don't have to explicity enqueue the dependencies for a script. For example: custom would also load gmap and gmaps_google, and fancybox would also load jquery. – Zach Lysobey Jun 3 '12 at 18:42
That is correct - and I mention this in my answer – Stephen Harris Jun 3 '12 at 19:06

Your Answer


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.