Load jQuery Files In WordPress

There’s different ways you can load scripts in WordPress. It really depends on whether the script is for use in a theme, plugin or whether you want it to only load on specific pages.

Clearly, the easiest way to use JavaScript or jQuery in a theme like Genesis is to simply paste it into one of the custom meta boxes for scripts.

In Genesis, you’ll find 2 boxes which you can use to:

  1. Load scripts globally – Site Wide
  2. Load scripts conditionally on specific pages and posts

Load Scripts Globally – Site Wide

There’s at least 2 ways you can add javascript or any other script to your site so it can be used globally.

Here’s the Header and Footer Scripts box you’ll find in Genesis > Theme Settings

header footer scripts

These boxes are generally used for adding scripts like Google Analytics or live chat scripts.

Note: Any jQuery or Javascript added to these boxes generally needs to be wrapped in opening and closing script tags.

2nd (Preferred Method) – Using wp_enqueue_scripts

If your theme doesn’t include boxes like these, you can add some code to your child themes functions.php file.

The above example loads a script which isn’t dependant on jQuery.

Load jQuery Dependant Scripts

Here’s an example which loads a script dependant on jQuery from a child themes functions file.

add_action( 'wp_enqueue_scripts', 'ambiance_load_scripts_styles' );
function ambiance_load_scripts_styles() {
    wp_enqueue_script( 'script-handle', get_bloginfo( 'stylesheet_directory' ) . '/js/effects.js', array( 'jquery' ), '1.0.0' );
}

Note the addition of array( 'jquery' ) which tells WordPress the script requires jQuery.

Once you have loaded your script correctly, you also need to make sure the script is added in the file correctly like this example:

jQuery(function( $ ){

// Your jQuery

});

Note the jQuery(function( $ ){ added to the start of the jQuery.

You can also use the following method :

( function() {

// Your jQuery

})( document, jQuery );

And this :

jQuery(document).ready(function($) { 

// Your jQuery

});

Once you have registered your script, you can also call it conditionally using the handle only so it only loads on the pages you need.

Another option is to enqueue your script directly in a template file like a front-page.php file rather then your functions file so it doesn’t load on every page.

Load Scripts Conditionally – Specific Pages & Posts

A theme framework like Genesis makes customizing WordPress easy by enabling you to load scripts on any specific page or post.

You’ll find a scripts box under every edit post/page screen where you can simply paste in your script.

If you can’t find the box for Scripts, click the Screen Options tab in the top right and check the box to enable the Scripts box to display.

page specific script

If your theme doesn’t include a Scripts box, add this code to the end of your child themes functions.php file and replace the page i’d with the page-slug or i.d you want the script to work on.

You can also change the conditional tag to suit your needs.

Loading Scripts In Parent Theme

The only difference between loading scripts in your parent theme compared to your child theme is:

get_template_directory_uri() - Use this for parent themes.
get_stylesheet_directory_uri() - Use this for child themes.

This code should be used for loading scripts conditionally in a parent theme.

Loading Scripts Using Code

Simply create a new folder in your child themes root directory and place your.js file in there.

This code assumes your .js file is named your-script. If you change it, you will also need to change the names and file paths in the code.

Loading Scripts For Custom Background Image

Here’s the code used in the popular Metro theme by StudioPress.

You could use this code in other Genesis child themes for adding a responsive background image to your website.

Loading Scripts in Plugins Manually

Most plugins which include scripts, also include the code to load the script so you don’t need to do anything.

But what if you find a plugin which only includes the raw code or PHP script? In this case you may need to load it in WordPress so this tutorial will help you do that.

If you’re new to plugin Development, here’s the correct method to load jQuery scripts for plugins.

Loading scripts using best practice includes using the same method as loading style sheets which also include the wp_register and wp_enqueue functions to enqueue a style.css file correctly.

Simple Scripts Plugin

If your theme doesn’t offer a field to add scripts, you can simply install this plugin name Simple Scripts.

add scripts

Do You Know The Answer To This Question

Is wp_enqueue_scripts a function or a hook?


Comments

8 responses to “Load jQuery Files In WordPress”

  1. Good stuff!

    Would love to see how we could add a script box (like Genesis) to other themes!

    1. Brad Dalton Avatar
      Brad Dalton

      What theme are you using?

  2. scott V Avatar

    Hey Brad,
    thanks for posting this stuff, here its a big help to a quasi-newbie like me.
    I am using Genesis ( Metric theme ) and rather than pasting a script directly into the Header/Footer
    box provided I add an add action for the “wp_enqueue_scripts” you mention above to my functions.php file. I then added a folder called js to my child theme directory and placed the js file in that.
    When I look at the site now it runs fine , but if I look at the source code I see no mention of the wp-enqueue, or the script referenced. My question should I see that code being referenced in the source code, or is it bundled with something else so I would not necessarily see a mention of the wp_enqueue_scripts when looking at the source code.

    I understand basically how and why to add the script you want this way but by know means am I sure if I am doing it correctly, so hoping that you might be able to take a look at the source code here for the site and see if you see something in the source code that indicates

    I know you want people to use GitHub to post code so I did not post any here- as mentioned above I am a quasi newbie- so trying to read a tutorial now on how to use GitHub

    Best
    Scott V

    1. Brad Dalton Avatar
      Brad Dalton

      Hi Scott

      You will see the HTML output but not the PHP and you should also see the Javascript.

      Github Gists are excellent for hosting code however you rely on them and the code isn’t hosted on your site which has pro’s and con’s.

  3. Wow, awesome resource Brad. Thanks!

    I’ve got a plugin (Layar Slider) that loads on each and every page, even though I only use it on one or two pages. Do you think I could use the tip called: Load Scripts Conditionally – Specific Pages & Posts to only load it on the pages I need? This would speed up my site quite a bit..

    Thanks a lot.

    Zak

    1. Brad Dalton Avatar
      Brad Dalton

      You can add a conditional tag after the function. Example:
      [code]
      if (is_front_page() )
      [/code]

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.