Loading WordPress Scripts/Styles Correctly

As a developer there are lots of times when developing WordPress themes when you want to add links to javascript and/or stylesheets in your themes to give extra functionality etc. The usual method of adding calls to these into the head of your header.php file is fine, but there is a better way, using a built in WordPress function. In this post I will outline the best ways to add these to your themes or plugins.

Often when building a theme you will want to add code to the head of your page such as javascript and stylesheet files. WordPress contains a couple of pretty cool functions that give you some additional functionality that just adding the code the head doesn’t give. The functions that you need are outlined below:

[php]
wp_register_script()
wp_enqueue_script()
wp_register_style()
wp_enqueue_style()
[/php]

As you can see there are two functions associated with scripts and two with styles. The wp_register_script() allows you to tell WordPress which scripts you want to use and the wp_enqueue_script() tells WordPress to add them to the head tag inside the wp_head() function. The same can be said for wp_register_style() and wp_enqueue_style().

Lets look at an example of you may want to use these in order to add a load a javascript file into your theme. In this example we will imagine that your javascript file is called slider.js and is located in your themes folder in a sub folder called scripts. The first thing we need to do is to register the script file which tells WordPress where the file is located. Here is the code for this:

[php]
<?php wp_register_style( ‘my_slider’, bloginfo(‘template_url’) . ‘/scripts/slider.js’ ); ?>
[/php]

As you can see above the first parameter in the wp_regsiter_script() function gives our javascript file a name. It is really important that this is a unique name and therefore I would highly recommend that like all functions you create you prefix your functions and names in functions.php. Here I have just prefixed it with the word ‘my’.

Now WordPress knows the location of the javascript file we need to use the wp_enqueue_script() function to actually add this to the head of the page. Here is the code for this:

[php]
<?php wp_enqueue_script( ‘my_slider’ ); ?>
[/php]

As you can see here we pass the name that we gave to our javascript file when we registered it to the wp_enqueue_script() so that WordPress adds it to the wp_head() part of your theme. That should do it and the style functions mentioned work in the same way. Register your style and give it a unique name and then add it with wp_enqueue_style(). The only other thing that you might want to do is to create a function for this. To do this the entire code would then become:

[php]
<?php
function my_scripts_styles() {
wp_register_style( ‘my_slider’, bloginfo(‘template_url’) . ‘/scripts/slider.js’ );
wp_enqueue_script( ‘my_slider’ );
}
add_action(‘init’, ‘my_scripts_styles’);
?>
[/php]

So I suppose you might be wondering why going through all that is better than adding them to the head of your header file in your theme yourself. Well the main reason is that using these functions, WordPress checks to see if any other plugin etc has already loaded the files that you are asking for and then if it has it does not load them twice. If they were added manually then there is a chance that javascript files could be loaded twice which could lead to errors.

I was inspired to write this post after watching Chelsea Otakan presentation at WordCamp Pheonix on Typography and WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *