Common Mistake When Adding Scripts and Stylesheets in WordPress
Many new WordPress plugins and theme developers make the mistake of directly adding their scripts or inline CSS into their plugins and themes.
Some mistakenly use the wp_head function to load their scripts and stylesheets.
Tables can't be imported directly. Please insert an image of your table which can be found here.
1 2 3 4 5 6
While the above code may seem easier, it is the wrong way of adding scripts in WordPress, and it leads to more conflicts in the future.
For example, if you load jQuery manually and another plugin loads jQuery through the proper method, then you have jQuery being loaded twice. If it is loaded on every page, then this will negatively affect WordPress speed and performance.
It is also possible that the two are different versions which can also cause conflicts.
That being said, let’s take a look at the right way of adding scripts and stylesheets.
Why Enqueue Scripts and Styles in WordPress?
WordPress has a strong developer community. Thousands of people from around the world develop themes and plugins for WordPress.
To make sure that everything works properly, and no one is stepping on another’s toes, WordPress has an enqueuing system. This system provides a programmable way of loading JavaScripts and CSS stylesheets.
By using wp_enqueue_script and wp_enqueue_style functions, you tell WordPress when to load a file, where to load it, and what are its dependencies.
This system also allow developers to utilize the built-in JavaScript libraries that come bundled with WordPress rather than loading the same third-party script multiple times. This reduces page load time and helps avoid conflicts with other themes and plugins.
How to Properly Enqueue Scripts in WordPress?
Loading scripts properly in WordPress is very easy. Below is an example code that you would paste in your plugins file or in your theme’s functions.php file to properly load scripts in WordPress.
Tables can't be imported directly. Please insert an image of your table which can be found here.
1 2 3 4 5 6 7 8 9 10
Explanation:
We started by registering our script through the wp_register_script() function. This function accepts 5 parameters:
- $handle – Handle is the unique name of your script. Ours is called “my_amazing_script”
- $src – src is the location of your script. We are using the plugins_url function to get the proper URL of our plugins folder. Once WordPress finds that, then it will look for our filename amazing_script.js in that folder.
- $deps – deps is for dependency. Since our script uses jQuery, we have added jQuery in the dependency area. WordPress will automatically load jQuery if it is not being loaded already on the site.
- $ver – This is the version number of our script. This parameter is not required.
- $in_footer – We want to load our script in the footer, so we have set the value to be true. If you want to load the script in the header, then you would make it false.
After providing all the parameters in wp_register_script, we can just call the script in wp_enqueue_script() which makes everything happen.
The last step is to use wp_enqueue_scripts action hook to actually load the script. Since this is an example code, we have added that right below everything else.
If you were adding this to your theme or plugin, then you can place this action hook where the script is actually required. This allows you to reduce the memory footprint of your plugin.
Now some might wonder why are we going the extra step to register the script first and then enqueuing it? Well, this allows other site owners to deregister your script without modifying the core code of your plugin.
Properly Enqueue Styles in WordPress
Just like scripts, you can also enqueue your stylesheets. Look at the example below:
Tables can't be imported directly. Please insert an image of your table which can be found here.
1 2 3 4 5 6 7
Instead of using wp_enqueue_script, we are now using wp_enqueue_style to add our stylesheet.
Notice that we have used wp_enqueue_scripts action hook for both styles and scripts. Despite the name, this function works for both.
In the examples above, we have used plugins_url function to point to the location of the script or style we wanted to enqueue.
However, if you are using the enqueue scripts function in your theme, then simply use get_template_directory_uri() instead. If you are working with a child theme, then use get_stylesheet_directory_uri().
Below is an example code:
Tables can't be imported directly. Please insert an image of your table which can be found here.
1 2 3 4 5 6 7 8 9