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, $args );
$handle(string) (required)
Name of the script. Should be unique.
$src(string|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
$args(array|true|false)

An array of additional script loading strategies. Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.

Default: empty array

  • strategy(string)
    Optional. If provided, may be either 'defer' or 'async'.

  • in_footer(true|false)
    Optional. Whether to print the script in the footer.
    Default: 'false'

Examples

2

#1 How to connect jquery from Google

Read in separate article.

2

#2 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

#3 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

#4 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

#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.
Since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.

wp_register_script() code WP 6.5.2

function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args = array() ) {
	if ( ! is_array( $args ) ) {
		$args = array(
			'in_footer' => (bool) $args,
		);
	}
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	$wp_scripts = wp_scripts();

	$registered = $wp_scripts->add( $handle, $src, $deps, $ver );
	if ( ! empty( $args['in_footer'] ) ) {
		$wp_scripts->add_data( $handle, 'group', 1 );
	}
	if ( ! empty( $args['strategy'] ) ) {
		$wp_scripts->add_data( $handle, 'strategy', $args['strategy'] );
	}
	return $registered;
}