WordPress Enqueue Scripts: What is the best way to put your resources in queue on WordPress

Jun 30, 2022
wp_enqueue_scripts

In WordPress instead of just putting these headers in your site, it is recommended to make use of the enqueueing function as it is the most commonly used method to control the assets you have. Additionally, it has advantages in the management of dependencies. Discover how through the scripts wp_enqueue_scripts.

How Enqueueing Works

Two main steps are taken to queue up scripts a fashion. First, you must register the script, notifying WordPress that it's registered and following which you place the script in the queue prior to putting the script into the header tag or right before close the body tag.

The reason for having two steps is because it aids in the flexibility. In certain situations, it is necessary to let WordPress inform them about assets. It is possible that you do not wish to use the feature on every webpage. For instance for an exclusive gallery shortcode which uses Javascript the sole requirement is for the loading of JS when it is utilized and probably not on every page.

Queuing Basics Using wp_enqueue_scripts

For enqueuing scripts and style on the front end of the site You'll have utilize the scripts wp_enqueue_scripts hook. Within the hooked function you can use the wp_register_script(), wp_enqueue_script(), wp_register_style() and wp_enqueue_style() functions.

add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ); function my_plugin_assets() wp_register_style( 'custom-gallery', plugins_url( '/css/gallery.css' , __FILE__ ) ); wp_register_script( 'custom-gallery', plugins_url( '/js/gallery.js' , __FILE__ ) ); wp_enqueue_style( 'custom-gallery' ); wp_enqueue_script( 'custom-gallery' ); 

In the previous example, I registered and queued resources inside the same program, which may be a bit redundant. You can employ the enqueue function in order to sign up and queue immediately, by using the same arguments you use in the register.

add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ); function my_plugin_assets() wp_enqueue_style( 'custom-gallery', plugins_url( '/css/gallery.css' , __FILE__ ) ); wp_enqueue_script( 'custom-gallery', plugins_url( '/js/gallery.js' , __FILE__ ) ); 
add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ); function my_plugin_assets() wp_register_style( 'custom-gallery', plugins_url( '/css/gallery.css' , __FILE__ ) ); wp_register_script( 'custom-gallery', plugins_url( '/js/gallery.js' , __FILE__ ) ); add_shortcode( 'custom_gallery', 'custom_gallery' ); function custom_gallery( $atts ) wp_enqueue_style( 'custom-gallery' ); wp_enqueue_script( 'custom-gallery' ); // Gallery code here 

Dependency Management

WordPress is a platform that runs in the internet and comes with built-in controls for dependency making an use of the third argument both of WordPress registration format() and the wp_register_script() functions. In addition, you are able to use the functions that allow you to wait for now, in the event that you are not segregating these two functions.

The last parameter provides an inventory of registered styles and scripts to load before the asset can be queued. In the example, it is likely to be based on JQuery. We'll interpret this as follows:

add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ); function my_plugin_assets() wp_enqueue_script( 'custom-gallery', plugins_url( '/js/gallery.js' , __FILE__ ), array( 'jquery' ) ); 

You don't have to register or to enqueue JQuery on our own since JQuery is already a part of WordPress. You'll be able to locate the complete listing of themes and scripts which are available for WordPress on the Codex.

Are you interested in knowing the ways we have increased number of visitors by over 1000 percent?

Join the 20,000+ that receive our newsletter each month that includes insider WordPress advice!

If you've got dependencies on your own, you'll have to include them in the process or else your scripts will not run. Here's an example.

add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ); function my_plugin_assets() wp_enqueue_script( 'custom-gallery', plugins_url( '/js/gallery.js' , __FILE__ ), array( 'jquery' ) ); wp_enqueue_script( 'custom-gallery-lightbox', plugins_url( '/js/gallery-lightbox.js' , __FILE__ ), array( 'custom-gallery' ) ); 

Let's say that the initial script includes a gallery. It's an addition to the script that allows images to open within an open lightbox. Take note that even though our script 2 relies on jQuery, there's not a need to mention this dependency as our initial script has already been loaded with jQuery. That said, it may be beneficial to specify each dependency to make sure that there's no problems if you don't make the necessary changes.

WordPress knows about the required scripts and determines the order in which they will require to be included on the page.

Queuing systems are a great way for adding scripts to the footer using the fifth parameter (the fourth parameter can be used to add the code). The scenario we've provided below would incorporate scripts inside the footer in the event that we change the footer's appearance in a subtle.

add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ); function my_plugin_assets() wp_enqueue_script( 'custom-gallery', plugins_url( '/js/gallery.js' , __FILE__ ), array( 'jquery' ), '1.0', true ); wp_enqueue_script( 'custom-gallery-lightbox', plugins_url( '/js/gallery-lightbox.js' , __FILE__ ), array( 'custom-gallery', 'jquery' ), '1.0', true ); 

If you specify that the parameter is true, then the fifth will put scripts inside the footer. By using false, or leaving the parameter out, you will place things inside the header. Like I said before in the event that it is possible, you must include scripts in the footer.

The art of specifying the medium for design

With the help of the style register/enqueue function, which has five parameters, you will be able control the media type the script was designed for (print or screen, as well as handheld gadgets.). This function will permit you to restrict the use of the style to a particular type of media, making it a powerful technique for optimizing your workflow.

add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ); function my_plugin_assets() wp_register_style( 'custom-gallery-print', plugins_url( '/css/gallery.css' , __FILE__ ), array(), '1.0', 'print' ); 

For a comprehensive description of every type of media which can be employed to make a site, read the CSS specifications.

Summary

Queuing assets is an effective method of managing this type of asset. It gives the user, together with other plugin developers and themes, better control of the platform since it's in a position to take the responsibility of dependency out of your hands.

As if that wasn't enough it's the method to integrate your assets, many themes marketplaces and WordPress repository will not approve of your work If you do not make use of this process.

Cut down on time and costs and increase the efficiency of your website by:

  • Assistance is always available 24 hours a day, 7 days a week. assistance from WordPress experts in hosting. It is accessible 24/7.
  • Cloudflare Enterprise integration.
  • Global reach with 34 data centers located around the globe.
  • Optimization with the Application's integrated Performance Monitoring.

The post was published on this website.

This post was posted on here