Add Javascript and CSS files to the Head and Footer in WordPress

Getting it right in the head

javascript-header-wordpressAs a hack every now and then I used to add in javascript or CSS files with links hardcoded directly into the header.php or footer.php template files of a WordPress theme.

This really is a no no! as this can result in script conflicts or duplications or just downright badness,  there is a logical method to add all javascript and CSS files and for them to be recognised properly by WordPress and loaded safely in either the header or footer of the page and to have any other dependent scripts also loaded without conflict.

Register and Enqueue

The correct method in WordPress to load javascripts and CSS files is to register the scripts and then to enqueue the script. The register process; wp_register_script() for Javascript or wp_register_style() for CSS, basically tells WordPress about the script, and the enqueue process; wp_enqueue_script () for Javascript or wp_enqueue_style() for CSS, tells WordPress to run/load  the script.

The difference between register and enqueue is subtle and in fact you could just get by with just using the enqueue step by itself but for sake of doing it the proper way lets use both processes.

Function and functions.php

So where do you add in the scripts and CSS?, well thats all done in your functions.php file of your theme located in the theme folder and added in as a unique function. If you are using a child theme which you should be and can’t see a functions.php file then just create one and ensure the file starts with an opening php tag.

<?php

Let’s create a function in functions.php that will store all our javascripts and CSS files to use and with that function we need to instruct WordPress to load the function by adding an action.

function mythemename_all_scriptsandstyles {
//Load JS and CSS files in here

}

add_action( 'wp_enqueue_scripts', 'mythemename_all_scriptsandstyles' );

So the steps are to create a function name that is unique, prefixing it with a theme name abbrevaition and descriptive words are a good idea in this case mythemename_all_scriptsandstyles, surrounding the function in a curly brace set to later put in the scripts and then to add an action to call the function to load all the scripts and styles, the action; add_action is using the WordPress PHP function wp_enqueue_scripts to load all the scripts and styles at once in my new function; mythemename_all_scriptsandstyles

So the wp_enqueue_script is used for each individual script and then collectively in the action.

Adding in a Javascript File

Ok lets load a Javascript file in, plenty to chose from, I use placeholder.js a lot for legacy browsers to display default text in input form fields and also, add the javascript in a js folder in your theme’s folder

function mythemename_all_scriptsandstyles {
//Load JS and CSS files in here

  wp_register_script ('placeholder', get_stylesheet_directory_uri() . '/js/placeholder.js', array( 'jquery' ),'1.0.0',true);

  wp_enqueue_script('placeholder');
}

add_action( 'wp_enqueue_scripts', 'mythemename_all_scriptsandstyles' );

So by registering the script with wp_register_script(), you pass in 5 arguments separated by commas, 2 of which are compulsory for it to work:

  1. the name – whatever you like, best to be similar to the script – compulsory
  2. the location – where the script is stored –  compulsory
  3. any script dependencies – what other scripts if any are required – optional
  4. the version – you can name this whatever you like, best to name it what the version actually is –  optional – defaults to WordPress version number
  5. header or footer locationtrue for footer, false for header –  optional – defaults to header

Script Dependencies

If your script is dependent on other scripts be sure to find out what they are and also register and enqueue them too, WordPress comes with a bunch of scripts so check to make sure that it may already be available, the full list is in the WordPress Codex here. So if your scripts are dependent on other scripts that are already registered then add these in the array() format  as above the array can include multiple dependent scripts just separated by commas in the array. 

 

Adding in a CSS File

Now lets add in a CSS file and use a Google font as an example.

function mythemename_all_scriptsandstyles() {
//Load JS and CSS files in here
  wp_register_script ('placeholder', get_stylesheet_directory_uri() . '/js/placeholder.js', array( 'jquery' ),'1.0.0',true);
  wp_register_style ('googlefonts', 'http://fonts.googleapis.com/css?family=Hammersmith+One', array(),'1.0.0','all');

  wp_enqueue_script('placeholder');
  wp_enqueue_style( 'googlefonts');
}

add_action( 'wp_enqueue_scripts', 'mythemename_all_scriptsandstyles' );

First thing is to remember to change the WordPress PHP function names to style instead of script; wp_register_style() and wp_enqueue_style()

The wp_register_style() also has 5 parameters;

  1. the name – whatever you like, best to be similar to the style – compulsory
  2. the location – where the style is stored –  compulsory
  3. any dependent stylesheets – what other styles if any are required – optional
  4. the version – you can name this whatever you like, best to name it what the version actually is –  optional – defaults to WordPress version number
  5. media type – for example, ‘all’, ‘print’, ‘handheld’ –  optional – defaults to all

Its more likely just to pass in the first 2 parameters for CSS files. Remember to also declare the wp_enqueue_style function after, passing in the name of the style googlefonts in the above instance.

Adding a Script to Only the Home Page

You can also selectively declare if you just want a style of script to load just on a certain page. This example will use the smooth-scroll,js script just to be on the home page only.

function mythemename_all_scriptsandstyles() {
//Load JS and CSS files in here
  wp_register_script ('placeholder', get_stylesheet_directory_uri() . '/js/placeholder.js', array( 'jquery' ),'1',true);
  wp_register_style ('googlefonts', 'http://fonts.googleapis.com/css?family=Hammersmith+One', array(),'2','all');
  wp_register_script ('smoothscroll', get_stylesheet_directory_uri() . '/js/smooth-scroll.js', array( 'jquery' ),'1',true);

  wp_enqueue_script('placeholder');
  wp_enqueue_style( 'googlefonts');
  if ( is_front_page() ) {
  wp_enqueue_script('smoothscroll');
  }

}
add_action( 'wp_enqueue_scripts', 'mythemename_all_scriptsandstyles' );

So here the smooth-scroll.js file is registered normally with wp_register_script, but in the wp_enqueue_script function it is being conditionally loaded to only the home page. This is done by adding in an if statement with a conditional tag  wrapped around the wp_enqueue_script function with a pair of curly braces.

There you have it, by keeping all the scripts and styles under one generic function makes managing everything a lot simpler and easy to use.

12 Comments

  1. Vapokings on September 22, 2017 at 7:58 pm

    Are missing the () on the function

  2. DARS Photography on September 19, 2017 at 11:32 pm

    Is there a way to add Schema Navation Element using a function rather than . My website’s theme isn’t compatible with HTML for some reason. Thanks for your help!

  3. Wassup on June 23, 2017 at 8:36 am

    code error – function mythemename_all_scriptsandstyles needs ()

  4. Sono on November 7, 2016 at 1:57 pm

    Could you show me step by step to implement particles.js in wordpress theme?
    Thanks

  5. favourite movie on August 11, 2016 at 11:27 am

    Excellent blog here! Also your website lots up fast! What host are you the use of?
    Can I am getting your affiliate hyperlink in your host? I desire my website loaded up as quickly as yours lol.

  6. ionutforyou on March 23, 2016 at 8:40 pm

    Thank you

  7. ts on June 17, 2015 at 2:42 pm

    Please do a video tutorial so people who know zero PHP can do this too!

  8. Francisco on January 4, 2015 at 4:05 pm

    Excellent! Thank you very much. Just one think: you forget put “()” after the function “mythemename_all_scriptsandstyles”. So, the correct way is mythemename_all_scriptsandstyles() to avoid errors.

    Bye!

  9. vitmaj on December 6, 2014 at 9:09 am

    Thanks buddy…wonderfull….it helped me…

Leave a Comment





%d bloggers like this: