751 posts
  • 6 Years of Membership
  • Affiliate Level 1
  • Collector Level 2
  • Exclusive Author
+1 more
CyberShot
says

I am not sure what to do here. The codex doesn’t have a bunch of information. When loading scripts into your WordPress theme, do you have to register the script first or can you just enqueue it? Both seem to do the same thing.

3535 posts
  • Power Elite Author
  • Author Level 12
  • Trendsetter
  • United States
+13 more
sevenspark
Moderator
says

Registering it just registers the map of $handle to $src. Registering a script will not automatically load it on your site.

Enqueue actually queues the script to be loaded into the markup.

Enqueue will automatically do the registering, so generally you don’t need to use wp_register_script at all.

For registered scripts, you can enqueue them simply by calling wp_enqueue_script on the $handle, without passing the other arguments.

Hope that clears it up :)

EDIT

In other words,

//register and enqueue myscript in one step
wp_enqueue_script( 
  'myscript',
  'path/to/myscript.js',
  array( 'jquery' ),
  '1.2',
  true
);

is equivalent to

//register myscript (tell WordPress it exists and where to find it)
wp_register_script( 
  'myscript',
  'path/to/myscript.js',
  array( 'jquery' ),
  '1.2',
  true
);
//enqueue myscript (actually add it to the page)
wp_enqueue_script( 'myscript' );
751 posts
  • 6 Years of Membership
  • Affiliate Level 1
  • Collector Level 2
  • Exclusive Author
+1 more
CyberShot
says

it does, although still a little confusing. I just don’t see any reason to have wp_register_script if enqueue does the same thing.

905 posts
  • Author Level 3
  • Affiliate Level 1
  • Collector Level 2
  • 4 Years of Membership
+4 more
infuse01
says

There are some situations where this could come in handy – for example if your theme or plugin has used a script for quite a while and other plugins depends on this script > Compatibility.

But in most cases wp_enqueue_script() is enough

709 posts
  • Affiliate Level 2
  • Author Level 5
  • Beta Tester
  • Collector Level 2
+2 more
ChillThemes
says

Here’s an article about it: http://wpcandy.com/teaches/how-to-load-scripts-in-wordpress-themes

Using wp_register_script() and wp_enqueue_script() together also makes things more organized and easier to keep track of. I personally, just use wp_enqueue_script()

751 posts
  • 6 Years of Membership
  • Affiliate Level 1
  • Collector Level 2
  • Exclusive Author
+1 more
CyberShot
says

some good information. Thanks guys

3535 posts
  • Power Elite Author
  • Author Level 12
  • Trendsetter
  • United States
+13 more
sevenspark
Moderator
says

One other reason register_script exists is so that WordPress can register all of the scripts that it packages in the core product and allow the developer to simply call wp_enqueue_script( ‘jquery’ ) for instance, without worrying about src paths, etc.

751 posts
  • 6 Years of Membership
  • Affiliate Level 1
  • Collector Level 2
  • Exclusive Author
+1 more
CyberShot
says

Now that is the reason that prompted the “aha” moment in my head.

by
by
by
by
by
by