Adding Scripts And Styles into WordPress

0

If you will want to add your own styling or JavaScript functionality. This can be done by enqueueing the asset in question. Enqueuing uses actions to add scripts and styles modularly, taking care of any dependencies in the process. Let’s add support for a Google Font, which is actually a stylesheet:

In the hooked function we use wp_enqueue_style() to add our style. The first parameter is the slug or handle of the script (this is up to you), and the second parameter is the URL of the script.

Using //url.com instead of http://url.com is a neat trick which allows browsers to grab the appropriate version of the script. If your connection uses https it will retrieve the HTTPS version, otherwise it will use the regular HTTP version.

You can, of course, load assets you’ve made and store within your plugin. Let’s load a custom script we’ve made using the enqueueing method:

The process is much the same but I’ve used more parameters in thewp_enqueue_script() function. The third parameter defines the dependencies of the script. WordPress makes sure to load all the dependencies properly so even if you enqueue a dependency later they will be loaded correctly. The fourth parameter is a version number you can choose yourself. These additional parameters are available for the wp_enqueue_style() function as well.

Share.