wp_enqueue_script()
function was the proper way of adding script in WordPress, I came to know about one other function wp_register_script()
. Having read through some articles I then decided to use wp_register_script()
and wp_enqueue_script()
simultaneously for each of my scripts. That is I first registered and then enqueued the script. Just like this:wp_register_script( 'owlcarousel', get_theme_file_uri('/assets/js/owl.carousel.min.js'), array( 'jquery' ), '2.2.0', true );
wp_enqueue_script( 'owlcarousel' );
But a few days back I decided to review my research result. That time I got an article (last updated on 8th November, 2017) from wpbeginner in my hand, which said that –
Now some might wonder why are we going to 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.Reference Article:
https://www.wpbeginner.com/wp-tutorials/how-to-properly-add-javascripts-and-styles-in-wordpress/
According to the above article we can register all of our scripts for the prospective developers who might want to deregister those. But I wanted to carry out some further research. Some info could be brought up from the subsequent references -
(wp_enqueue_script()) Registers the script if $src provided (does NOT overwrite), and enqueues it. 1
In fact, you could just use the ‘enqueue’ function and never use the ‘register’ function. The ‘enqueue’ (wp_enqueue_script())function technically ’registers’ the script. 2
If you try to register or enqueue an already registered handle with different parameters, the new parameters will be ignored. Instead use wp_deregister_script(), and register the script again with the new parameters. 1
An example of when we would do this i.e., using wp_register_script() instead of wp_enqueue_script(), would be for JavaScript we wanted to add if the page content had a shortcode that needed it. We won't know if a shortcode is in the content of the page/post until long past the wp_head action has run. 2
//if part of theme, in the theme's functions.php
add_shortcode( 'jht_ideas', 'jht_ideas_app' );
add_action( 'wp_enqueue_scripts', 'jht_enqueue_scripts' );
function jht_enqueue_scripts() {
wp_register_script('jhtscripts', get_bloginfo( 'stylesheet_directory' ) . '/js/dist/app.js', array('jquery'),'1.0.0',true);
// 'true' here says put script in footer if possible.
// 'array('jquery')' says this script depends on jquery.
}
function jht_ideas_app($atts, $content=null) {
// do something here...
wp_enqueue_script('jhtscripts');
// tell WP we want the jhtscripts on the page.
$output = '<div id="jhtMain"></div>';
return $output;
}
Reference Articles:
- https://developer.wordpress.org/reference/functions/wp_enqueue_script/
- https://jhtechservices.com/wordpress-wp_enqueue_script-vs-wp_register_script/
wp_enqueue_script()
itself registers the script and the enqueued script can be deregistered and reregistered with new parameters whenever needed. And also, from the code snippet, we learn that we could register our script using wp_register_script
only when we don’t want that script to be loaded at once and the script might be loaded later on using wp_enqueue_script()
, if needed.
In addition to that, I wanted to go over another article for more preciseness. And I found the following:
wp_register_script() registers a script to be enqueued later using the wp_enqueue_script() function.
Scripts that have been pre-registered using wp_register_script() do not need to be manually enqueued using wp_enqueue_script() if they are listed as a dependency of another script that is enqueued. WordPress will automatically include the registered script before it includes the enqueued script that lists the registered script’s handle as a dependency.
Registering scripts is technically not necessary, but highly recommended nonetheless.
If the handle of a registered script is listed in the $deps array of dependencies of another script that is enqueued with wp_enqueue_script(), that script will be automatically loaded prior to loading the enqueued script. This greatly simplifies the process of ensuring that a script has all the dependencies it needs.
So, the main purpose of the register functions is to allow you to simplify your code by removing the need to duplicate code if you enqueue the same script or style in more than one section of code.Reference article: https://developer.wordpress.org/reference/functions/wp_register_script/
Now what? Well, to be much more clear I went over this stackexchange’s post: https://wordpress.stackexchange.com/questions/82490/when-should-i-use-wp-register-script-with-wp-enqueue-script-vs-just-wp-enque
Reading so far, I think, your head’s got hot. To be frank, so has mine :P . Joking really.
Now the questions are-
Should we register a script first and then enqueue it, according to our first mentioned article? And is this a best practice?
Or, should we generally not register each script?
Well, now that we’ve read through and researched several articles, we can draw a conclusion.
- Registering any scripts using
wp_register_script()
is just registering; not loading it. The registered script will not be loaded until it is enqueued usingwp_enqueue_script()
. - We don’t need to register and enqueue each of the scripts simultaneously. We should just enqueue. Registering is not mandatory as the
wp_enqueue_script()
function automatically registers the script. - But we do need to register while we are in any of the following situations:
- Suppose we need a script to load into more than one place like once in front-end and once in back-end (admin page). Now we can register the script just once. And then enqueue it in front-end and back-end individually. Look, enqueueing means loading. Registering doesn’t mean loading. In case we don’t register it, it will be automatically registered as many times as we enqueue it. On the other hand, if we register it once, it will be registered once, no matter how many times we enqueue it.
- If we want to use a script as a dependency of other scripts, then we don’t need to enqueue it with
wp_enqueue_script()
. Just register it withwp_register_script()
. And it will be automatically enqueued when we use it’s handle name as a dependency of other scripts enqueued withwp_enqueue_script()
. - When we don’t want the script to be loaded at once rather than to be loaded if necessary, we can now just register it for later use. The use case is shown above.
wp_register_style()
and wp_enqueue_style()
in the same way.Note that the conclusion drawn here reflects my own deduction. If I’m wrong, please correct me. And you might have reached a different and much better conclusion. If thing’s so, then please let us know. Who knows? Perhaps yours is the best. As the saying goes, “As many tenets, as many ways to salvation.” :)