Methods to Use wp_enqueue_script to Enqueue Belongings in WordPress

Ad - Web Hosting from SiteGround - Crafted for easy site management. Click to learn more.

The wp_enqueue_scripts motion hook is a crucial element of the WordPress growth course of.

Along with the wp_enqueue_script() and wp_enqueue_style() capabilities, it helps WordPress output content material on the web site.

On this tutorial, we’ll cowl the wp_enqueue_scripts motion hook together with its complementary capabilities and supply numerous use instances that will help you enhance your WordPress tasks.

How Enqueueing Works in WordPress

Enqueueing in WordPress refers to registering and including scripts and stylesheets to your website.

The wp_enqueue_script() and wp_enqueue_style() capabilities instruct the WordPress server so as to add these scripts and stylesheets to a queue to load in your web site’s entrance finish.

The principle side of enqueueing is that it could possibly enhance web site efficiency by lowering web page load occasions and assist keep away from script conflicts when completely different plugins or WordPress themes attempt to load the identical script or stylesheet.

Understanding wp_enqueue_script

The enqueueing course of consists of the wp_enqueue_scripts WordPress hook and two further capabilities for stylesheets and scripts.

To start with, wp_enqueue_scripts is an motion hook used to enqueue stylesheets and JavaScript recordsdata in your WordPress website. This hook is normally utilized in a WordPress theme’s capabilities.php or plugin recordsdata.

In the meantime, wp_register_style() and wp_enqueue_style() are WordPress capabilities that work with stylesheets. The register capabilities to a stylesheet to make use of in a theme or plugin, whereas the enqueue operate masses a registered stylesheet to a WordPress web site.

By default, each of those capabilities have two predominant parameters:

  • $deal with – a singular title for a stylesheet to establish it inside the WordPress codebase. It should match the deal with you specified when registering the stylesheet.
  • $src – a URL or a path to the stylesheet file. This generally is a relative file path to a stylesheet within the WordPress theme or plugin listing or an absolute URL to a stylesheet hosted elsewhere. $src is non-compulsory and solely must be specified in case you didn’t achieve this within the wp_register_style() operate.

In consequence, it’s potential to make use of the wp_enqueue_style() operate with out utilizing the wp_register_style() first.

As an instance, right here’s code with each capabilities:

operate register_plugin_styles() {
	wp_register_style( 'plugin-development', plugins_url( '/css/plugin.css' ) );
	wp_enqueue_style( 'plugin-development' );
add_action( 'wp_enqueue_scripts', 'register_plugin_styles' );

Now, right here is the code with solely wp_enqueue_style():

operate register_plugin_styles() {
wp_enqueue_style( 'plugin-development', plugins_url( '/css/plugin.css' ) );
add_action( 'wp_enqueue_scripts', 'register_plugin_styles' );

Equally necessary, wp_register_script() and wp_enqueue_script() are WordPress capabilities chargeable for managing {custom} scripts. The register operate registers {custom} JavaScript recordsdata, whereas the wp_enqueue_script() operate masses the registered scripts to the web site. These capabilities each take two predominant parameters:

  • $deal with – distinctive title for the script. Do not forget that the deal with should match between wp_register_script() and wp_enqueue_script() capabilities.
  • $src – URL or path to the jQuery script file. That is non-compulsory for wp_enqueue_script() in case you already specified it on the register operate.

Furthermore, you possibly can specify three further parameters:

  • $deps – an array of different scripts that the present script is dependent upon. For instance, jQuery JavaScript recordsdata or json2.
  • $ver – the model variety of the script. Helpful you probably have a number of completely different scripts and need to monitor their variations.
  • $in_footer – determines whether or not to load the script within the footer. By default, WordPress masses scripts within the <head> part.

Equally to wp_enqueue_style(), you should utilize the wp_enqueue_script() operate with out registering it first.

Right here’s an instance with each capabilities:

operate register_plugin_script() {
	wp_register_script( 'new-script', plugins_url( '/script.js' ) );
	wp_enqueue_script( 'new-script' );
add_action( 'wp_enqueue_scripts', 'register_plugin_script' );

Now, let’s see an instance with a single wp_enqueue_script() operate:

operate register_plugin_script() {
wp_enqueue_script( 'new-script', plugins_url( '/script.js' ) );
add_action( 'wp_enqueue_scripts', 'register_plugin_script' );

Methods to Use wp_enqueue_script in WordPress

As we’ve coated the principle elements of the wp_enqueue_script() operate, it’s time to take a look at just a few well-liked use instances that will help you perceive it higher.

Methods to Use wp_enqueue_script With jQuery

The wp_enqueue_script() operate has a further array() parameter that lets customers specify wanted script dependencies.

operate my_custom_script() {
    wp_enqueue_script( 'registered-script', get_template_directory_uri() . '/js/my-script.js', array('jquery'), '1.0', true );
add_action( 'wp_enqueue_scripts', 'my_custom_script' );

Within the above code, we’ve used the wp_enqueue_scripts() operate to enqueue a script referred to as my-script.js.

We specified that it is dependent upon the jQuery library and can load within the web page’s footer. We additionally used the get_template_directory_uri() operate to get the URL of the present theme listing.

You may enhance your web site’s pace by loading scripts within the footer. By default, WordPress masses scripts in your website’s header part, which means they load earlier than the opposite content material.

This may end up in slower web page loading occasions because the browser waits for these scripts.

If you happen to transfer scripts to the footer part, the browser can show the content material first after which load scripts. Test the next instance for extra info:

operate plugin_assets() {
wp_enqueue_script( 'custom-script', plugins_url( '/js/my-script.js' , __FILE__ ), array( 'jquery' ), true );
add_action( 'wp_enqueue_scripts', 'plugin_assets' );

Right here, we’ve set the wp_enqueue_script() operate’s $in_footer parameter as true, which is able to enqueue the script within the footer as an alternative of <head>.

Methods to Use wp_enqueue_scripts to Specify Media for Kinds

The wp_register_style() and wp_enqueue_style() capabilities include a media parameter. It specifies the meant media kind for the stylesheet. By default, this parameter is ready as all, which means the stylesheet applies to all media sorts.

Right here’s how a modified operate seems to be:

operate my_custom_styles() {
  // Register {custom} stylesheet
  wp_register_style( 'my-styles', get_template_directory_uri() . '/css/my-styles.css', array(), '1.0', 'display screen' );
  // Enqueue {custom} stylesheet
  wp_enqueue_style( 'my-styles' );
// Add the hook to the wp_enqueue_scripts motion
add_action( 'wp_enqueue_scripts', 'my_custom_styles' );

On this instance, we’ve created a operate referred to as my_custom_styles that registered and enqueued a {custom} stylesheet referred to as my-styles.css.

We’ve additionally set the media parameter to display screen, which suggests the stylesheet applies to display screen media equivalent to desktop screens, laptops, or tablets.

You can too use completely different media sorts, equivalent to print for print media or handheld for handheld gadgets.


The wp_enqueue_scripts hook and complementary capabilities like wp_enqueue_script() wp_enqueue_style() are helpful for including {custom} scripts and kinds to your WordPress website simply and effectively.

On this tutorial, we’ve coated the WordPress enqueueing course of and offered numerous use case examples for wp_register_script(), wp_register_style(), wp_enqueue_script(), and wp_enqueue_style() capabilities.

We hope you discovered this tutorial helpful and perceive the enqueueing course of higher now. Ought to you’ve gotten any questions, try our WordPress guide or go away a remark under.


Ignas takes nice satisfaction in serving to individuals sort out even essentially the most complicated technical points. His present purpose is to put in writing easy-to-follow articles in order that these points won’t occur in any respect. Throughout his free time, Ignas likes to play video video games and repair up issues round his home.

Ad - WooCommerce hosting from SiteGround - The best home for your online store. Click to learn more.

#wp_enqueue_script #Enqueue #Belongings #WordPress

No Comments

Leave a Reply

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