wp_register_script()WP 2.1.0

Register a new script.

Registers a script to be enqueued later using the wp_enqueue_script() function.

No Hooks.

Return

true|false. Whether the script has been registered. True on success, false on failure.

Usage

wp_register_script( $handle, $src, $deps, $ver, $in_footer );
$handle(string) (required)
Name of the script. Should be unique.
$src(string|true|false) (required)
Full URL of the script, or path of the script relative to the WordPress root directory. If source is set to false, script is an alias of other scripts it depends on.
$deps(string[])
An array of registered script handles this script depends on.
Default: empty array
$ver(string|true|false|null)
String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
Default: false
$in_footer(true|false)
Whether to enqueue the script before </body> instead of in the <head>.
Default: 'false'

Examples

0

#1 Separate registration of the script and its connection

add_action( 'init', 'register_myscripts' );
add_action( 'wp_enqueue_scripts', 'add_myscripts' );

//register the script
function register_myscripts(){

	$url = get_template_directory_uri() .'/myscript.js';

	wp_register_script( 'myfirstscript', $url, [ 'jquery', 'jquery-ui' ], 'v2.0', true );
}

// connect the scripts (queue the output to html)
function add_myscripts() {

	wp_enqueue_script( 'myfirstscript' );
}
0

#2 Register and connect immediately

If we know beforehand that we need the script only on one page, we can register it immediately and connect it to the output with wp_enqueue_script():

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

function my_scripts_method() {

	$script_url = plugins_url( '/js/newscript.js', __FILE__ );

	wp_enqueue_script( 'custom-script', $script_url, ['jquery'], '1.0', true );
}

For more examples, see the wp_enqueue_script() function description.

0

#3 How to connect jquery from Google

Read in separate article.

0

#4 Connecting an external script (the easy way)

add_action( 'init', 'register_remote_scripts' );
add_action( 'wp_enqueue_scripts', 'add_remote_scripts' );

function register_remote_scripts(){

	wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , '', '', true );
}

function add_remote_scripts(){

	wp_enqueue_script( 'someScript-js' );
}

Or just:

add_action( 'wp_enqueue_scripts', 'add_remote_scripts' );

function add_remote_scripts(){

	wp_enqueue_script( 'someScript-js', 'https://domain.com/someScript.js' , '', '', true );
}
0

#5 Depend one script on another

In a case where first script should be loaded only if another second script is loaded:

add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );

function my_enqueue_scripts(){

	wp_register_script( 'first', get_template_directory_uri() . 'js/first.js' );

	wp_enqueue_script( 'second', get_template_directory_uri() . 'js/second.js', array( 'first' ) );
}

Here, first.js will be loaded before second.js only if second.js is enqueued/loaded.

Notes

Changelog

Since 2.1.0 Introduced.
Since 4.3.0 A return value was added.

Code of wp_register_script() WP 5.9.3

function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	$wp_scripts = wp_scripts();

	$registered = $wp_scripts->add( $handle, $src, $deps, $ver );
	if ( $in_footer ) {
		$wp_scripts->add_data( $handle, 'group', 1 );
	}

	return $registered;
}