WP_Script_Modules::register
Registers the script module if no script module with that script module identifier has already been registered.
Метод класса: WP_Script_Modules{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
$WP_Script_Modules = new WP_Script_Modules(); $WP_Script_Modules->register( $id, $src, $deps, $version, $args );
- $id(строка) (обязательный)
- The identifier of the script module. Should be unique. It will be used in the final import map.
- $src(строка) (обязательный)
- 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.
- $deps(массив)
- .
По умолчанию:array() - $version(строка|false|null)
- String specifying the script module version number. It is added to the URL as a query string for cache busting purposes. If
$versionis set to false, the version number is the currently installed WordPress version. If$versionis set to null, no version is added.
По умолчанию:false - $args(array<string, string|bool>)
An array of additional args.
По умолчанию:
empty array-
in_footer(true|false)
Whether to print the script module in the footer. Only relevant to block themes. Optional.
По умолчанию: 'false' - fetchpriority('auto'|'low'|'high')
Fetch priority. Optional.
По умолчанию: 'auto'
-
Список изменений
| С версии 6.5.0 | Введена. |
| С версии 6.9.0 | Added the $args parameter. |
Код WP_Script_Modules::register() WP Script Modules::register WP 7.1.2
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,
);
}
}