WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 32 down vote accepted

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
    case 'value2':
        wp_enqueue_script( 'my-handy-javascript' ); // needs the file
    case 'value3': // doesn't needs the file

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.

share|improve this answer
Thank you. That cleared it up a lot. So, do you think that TwentyTwelve doesn't bother to register first since it's going to load them for sure? – akTed Jan 21 '13 at 15:09
That sure could be a possibility. I don't know how the Twenty Twelve theme is build since I never use it, but they sure will have their reasons to enqueue in stead of register. – Mike Madern Jan 21 '13 at 15:14
The way I see it, the register function is by no means technically required, it just exists for convenience: it saves some typing work. If that is true, then this should be explicitly stated in the documentation. – Jan-Philip Gehrcke Jun 16 '15 at 20:41
In that sense, I disagree with "You can enqueue the script every time, wich costs more resources, or just enqueue the script when you need it". In your example code, you could remove the register call and instead have multiple equivalent full-fledged (with $src arg) calls to wp_enqueue_style(). This would just be more typing effort, but not result in larger resource requirements. Correct? – Jan-Philip Gehrcke Jun 16 '15 at 20:43
So, this is meaning, for load script or css file, is necessary using wp_enqueue_scripts or wp_enqueue_style function, with or without register function? – Lai32290 Oct 20 '15 at 1:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.