Wordpress - When should I use wp_register_script() with wp_enqueue_script() vs just wp_enqueue_script()?
The wp_register_script()
Codex page literally says:
A safe way of registering javascripts in WordPress for later use with
wp_enqueue_script()
.
This means, if you want to register your scripts, but not directly load them in your pages, you can register the files once, and then load them when you need them.
For example:
You have a switch statement wich loads some functionality, but two of three cases needs a particular javascript file, and one doesn't. You can enqueue the script every time, wich costs more resources, or just enqueue the script when you need it:
...
wp_register_script( 'my-handy-javascript', ... );
...
switch( $somevar ) {
case 'value':
wp_enqueue_script( 'my-handy-javascript' ); // needs the file
...
break;
case 'value2':
wp_enqueue_script( 'my-handy-javascript' ); // needs the file
...
break;
default:
case 'value3': // doesn't needs the file
...
break;
}
It is not necessary to register a script and then enqueue them, but it can provide some logic in your code if you register all the scripts you need somewhere in your functions.php
instead of everywhere in your code.
The Codex also tells the following:
Use the
wp_enqueue_scripts
action to call this function, oradmin_enqueue_scripts
to call it on the admin side.
This means that if you want to enqueue your script on the frond-end and in the back-end, you can register a script once, and then load it on the front-end with wp_enqueue_script
and in the back-end with admin_enqueue_script
.
This way you won't have the same enqueue recourse twice in one theme, plugin, widget or whatever.
IMHO the main advantage of using wp_register_script
before wp_enqueue_scripts
is illustrated in the following paragraph from Codex:
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.
If you believe you don't need this (e.g. because you are 100% sure that your script won't be involved in any dependency) you probably can go directly with wp_enqueue_scripts
, without a preliminary wp_register_script
.
I have gone through some articles and come to the following conclusion. I think it helps.
- 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:
a. 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.
b. 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()
.c. If we want the script to be loaded when necessary rather than to be loaded at once, we can just register it for later use. The use case has been shown above.
N.B. As far as I am convinced, the same conclusion can be drawn for the CSS stylesheets. I mean to say that we can use 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.” :)
https://ansrthemeaction.blogspot.com/2019/11/wpregisterscript-when-to-use.html