WP_Script_Modules::register( string $id, string $src,  $deps = array(), string|false|null $version = false,  $args = array() )

In this article

Registers the script module if no script module with that script module identifier has already been registered.

Parameters

$idstringrequired
The identifier of the script module. Should be unique. It will be used in the final import map.
$srcstringoptional
Full URL of the script module, or path of the script module relative to the WordPress root directory. If it is provided and the script module has not been registered yet, it will be registered.
string|bool> $args { Optional. An array of additional args. Default empty array.
@type bool $in_footer Whether to print the script module in the footer. Only relevant to block themes. Default 'false'. Optional.
@type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
}
$versionstring|false|nulloptional
String specifying the script module version number. Defaults to false.
It is added to the URL as a query string for cache busting purposes. If $version is set to false, the version number is the currently installed WordPress version.
If $version is set to null, no version is added.

Default:false

Source

public function register( string $id, string $src, array $deps = array(), $version = false, array $args = array() ) {
	if ( '' === $id ) {
		_doing_it_wrong( __METHOD__, __( 'Non-empty string required for id.' ), '6.9.0' );
		return;
	}

	if ( ! isset( $this->registered[ $id ] ) ) {
		$dependencies = array();
		foreach ( $deps as $dependency ) {
			if ( is_array( $dependency ) ) {
				if ( ! isset( $dependency['id'] ) || ! is_string( $dependency['id'] ) ) {
					_doing_it_wrong( __METHOD__, __( 'Missing required id key in entry among dependencies array.' ), '6.5.0' );
					continue;
				}
				$dependencies[] = array(
					'id'     => $dependency['id'],
					'import' => isset( $dependency['import'] ) && 'dynamic' === $dependency['import'] ? 'dynamic' : 'static',
				);
			} elseif ( is_string( $dependency ) ) {
				$dependencies[] = array(
					'id'     => $dependency,
					'import' => 'static',
				);
			} else {
				_doing_it_wrong( __METHOD__, __( 'Entries in dependencies array must be either strings or arrays with an id key.' ), '6.5.0' );
			}
		}

		$in_footer = isset( $args['in_footer'] ) && (bool) $args['in_footer'];

		$fetchpriority = 'auto';
		if ( isset( $args['fetchpriority'] ) ) {
			if ( $this->is_valid_fetchpriority( $args['fetchpriority'] ) ) {
				$fetchpriority = $args['fetchpriority'];
			} else {
				_doing_it_wrong(
					__METHOD__,
					sprintf(
						/* translators: 1: $fetchpriority, 2: $id */
						__( 'Invalid fetchpriority `%1$s` defined for `%2$s` during script registration.' ),
						is_string( $args['fetchpriority'] ) ? $args['fetchpriority'] : gettype( $args['fetchpriority'] ),
						$id
					),
					'6.9.0'
				);
			}
		}

		$this->registered[ $id ] = array(
			'src'           => $src,
			'version'       => $version,
			'dependencies'  => $dependencies,
			'in_footer'     => $in_footer,
			'fetchpriority' => $fetchpriority,
		);
	}
}

Changelog

VersionDescription
6.9.0Added the $args parameter.
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.