Pregunta

I'm having trouble understanding when you need to use wp_register_script(). Currently, I just use something like:

add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
function enqueue() {
    $handle = 'some-handle';
    $js = 'http://example.com/my.js';
    wp_register_script( $handle, $js );
    wp_enqueue_script( $handle );
}

I've done a lot of reading (Codex, blogs, etc.), but can't quite understand when I should register first, or when I should just enqueue. As an example, I noticed TwentyTwelve doesn't register any styles or scripts, it just enqueues them.

¿Fue útil?

Solución

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, or admin_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.

Otros consejos

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.

  1. Registering any scripts using wp_register_script() is just registering; not loading it. The registered script will not be loaded until it is enqueued using wp_enqueue_script().
  2. 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.
  3. 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 with wp_register_script(). And it will be automatically enqueued when we use it’s handle name as a dependency of other scripts enqueued with wp_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

Licenciado bajo: CC-BY-SA con atribución
scroll top