Enqueue Scripts and Styles in WordPress

Enqueue Scripts and Styles
Enqueue Scripts and Styles

If you are reading this post to find out how to add Javascript or CSS to your WordPress site you are on the right path. I remember my first post where I wanted to add a jQuery code, yeap, it didn’t work the first time. Actually I had to read many information and articles, but none of the was helping me, I had to add a classic Javascript file on the header of my theme, exactly the header.php file. It worked for a while but now is now working any more, guess why? Because I was not doing it the right way.

There is a way to add/enqueue these Javascript and CSS files into WordPresss. WordPress includes functions to Enqueue Scripts and Styles in a way that you can always be sure to have available those files. I am writing this article so that you can digest better the information found in the WordPress CODEX site.

Where to add/enqueue my Scripts

There are two places where you can add your Scripts and Styles. The first place is the readers/users side, It could be a landing page, a post, a search page, a custom post, a widget, header, footer, and many more places.

The second place where you can add Scripts and Styles is the admin area, for this area we can include the previous places, but if you have worked with WordPress you know that there are places where only an Administrator is able to interact like your WordPress Dashboard, editing a post, in your page settings for a specific Plugin configuration and many more.

So having said some places where you can add a Script and Style you know that with this you can add powering stuff into your site.

Steps to add/enqueue a Scripts and Styles

By default WordPress has its own libraries loaded to do many things like jQuery, but on this case you want to add your own magical Script into your site.

  • To add a Script or Style into your admin site with you use the admin_enqueue_scripts/admin_enqueue_style hook.
  • To add a Script or Style into your front page you use the wp_enqueue_scripts/wp_enqueue_scripts.
  • To add it in the login page you use login_enqueue_scripts/login_enqueue_style.

 

Let us look at the following example:

On the previous example we see that we are enqueuing our Scripts and Styles into the front page by hooking our functions into the wp_enqueue_scripts/wp_enqueue_style hook.

Adding the Scripts and Styles into all the admin areas

With the previous code we are adding a style resource in every admin page.

Correct way to Enqueue Script and Styles

Now that you have an Idea of how this works I will teach you the correct way to use enqueue scripts and styles. First of all, WordPress has function to register Scripts and Style so that you don’t have to pollute your site with Scripts and Style that are not needed, you Enqueue them on the exact moment.

wp_register_script

wp_register_script has 5 Arguments of which only $handle and $src are required and the rest are optional. I will give a brief description of the 5 arguments but if you want the complete detail you can go to the CODEX page.

  1. $handle – This is the unique name that you will use with your script in further functions like wp_enqueue_script.
  2. $src – This is the source where you have you file, usually you use plugins_url() for plugins and get_template_directory_uri() for themes.
  3. $deps – Sometimes you want to include some other functionality like a datepicker from the jQuery UI site, since WordPress is already loaded with this and is already registered you only add the dependency. Si the CODEX for all the dependencies loaded in WordPress..
  4. $ver – If you are loading a script that is already on WordPress but you want to use that file then for the sake of clashing script is better to use a version, the number is up to you.
  5. $in_footer – This is if you want to load the script on the footer, by default the script goes in the head, so send a true value to place it at the footer.

This is an example of the wp_register_script function.

After you register you script you are able to use it anywhere you want either using admin_enqueue_scripts, wp_enqueue_scripts or login_enqueue_scripts.

wp_register_style

wp_register_script has 5 Arguments of which only $handle and $src are required and the rest are optional. I will give a brief description of the 5 arguments but if you want the complete detail you can go to the CODEX page.

  1. $handle – This is the unique name that you will use with your style in further functions like wp_enqueue_style.
  2. $src – This is the source where you have you file, usually you use plugins_url() for plugins and get_template_directory_uri() for themes.
  3. $deps – On this case is an array of stylesheets that you want to use with this one that your are including.
  4. $ver – If you are loading a styles that are already on WordPress but you want to use that file then for the sake of clashing styles is better to use a version, the number is up to you.
  5. $media– String specifying the media for which this stylesheet has been defined. Examples: ‘all‘, ‘screen‘, ‘handheld‘, ‘print‘.

Enqueuing Script to a specific admin edit page.

On this example we are enqueuing the my_enqueue script in the pages where the user role is able to edit.

Where should I write this code?

By this point you might have an understanding of how to properly add/enqueue scripts and styles into your WordPress site, but you might be wondering where to add this code. The answer is it depends. Yes, it depends where you add the code but mostly if you are a Theme developer you could add this code on the functions.php file or if you are a Plugin developer you can add this code where you need it (for example in a settings page). If you still need a guide of where to add it please add your comment.

Final Comments

As you can see adding/enqueuing script into our WordPress site is not that difficult and also we have the power to control our styles and scripts so that we can have a with better performance by adding them only when is need it. Just remember to add scripts/styles you first register them with wp_register_script/wp_register_style and the enqueue them in the correct place with admin_enqueue_scripts, wp_enqueue_scripts or login_enqueue_scripts.