WP_Script_Modules
- Since
- 6.5.0
- Source
wp-includes/class-wp-script-modules.php:16
Core class used to register script modules.
Compatibility
- WordPress
- since 6.5.0
- 6.7.10
- 6.8.11
- 6.9.10
- 7.0.7
- 7.1.3
Present in every tracked release (6.7.10 to 7.1.3).
Hooks and filters fired · 2
Every hook that fires from inside WP_Script_Modules, in the order it appears in the class, grouped by the method that fires it.
Properties · 3
$registeredarray[]private- Holds the registered script modules, keyed by script module identifier.
$enqueued_before_registeredarray<string,private- Holds the script module identifiers that were enqueued before registered.
$a11y_availableboolprivate- Tracks whether the @wordpress/a11y script module is available.
Methods · 14
- register()Registers the script module if no script module with that script module identifier has already been registered.
- enqueue()Marks the script module to be enqueued in the page.
- dequeue()Unmarks the script module so it will no longer be enqueued in the page.
- deregister()Removes a registered script module.
- add_hooks()Adds the hooks to print the import map, enqueued script modules and script module preloads.
- print_enqueued_script_modules()Prints the enqueued script modules using script tags with type="module" attributes.
- print_script_module_preloads()Prints the the static dependencies of the enqueued script modules using link tags with rel="modulepreload" attributes.
- print_import_map()Prints the import map using a script tag with a type="importmap" attribute.
- get_import_map()Returns the import map array.
- get_marked_for_enqueue()Retrieves the list of script modules marked for enqueue.
- get_dependencies()Retrieves all the dependencies for the given script module identifiers, filtered by import types.
- get_src()Gets the versioned URL for a script module src.
- print_script_module_data()Print data associated with Script Modules.
- print_a11y_script_module_html()
Source code
class WP_Script_Modules { /** * Holds the registered script modules, keyed by script module identifier. * * @since 6.5.0 * @var array[] */ private $registered = array(); /** * Holds the script module identifiers that were enqueued before registered. * * @since 6.5.0 * @var array<string, true> */ private $enqueued_before_registered = array(); /** * Tracks whether the @wordpress/a11y script module is available. * * Some additional HTML is required on the page for the module to work. Track * whether it's available to print at the appropriate time. * * @since 6.7.0 * @var bool */ private $a11y_available = false; /** * Registers the script module if no script module with that script module * identifier has already been registered. * * @since 6.5.0 * * @param string $id The identifier of the script module. Should be unique. It will be used in the * final import map. * @param string $src Optional. 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. * @param array $deps { * Optional. List of dependencies. * * @type string|array ...$0 { * An array of script module identifiers of the dependencies of this script * module. The dependencies can be strings or arrays. If they are arrays, * they need an `id` key with the script module identifier, and can contain * an `import` key with either `static` or `dynamic`. By default, * dependencies that don't contain an `import` key are considered static. * * @type string $id The script module identifier. * @type string $import Optional. Import type. May be either `static` or * `dynamic`. Defaults to `static`. * } * } * @param string|false|null $version Optional. 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. */ public function register( string $id, string $src, array $deps = array(), $version = false ) { if ( ! isset( $this->registered[ $id ] ) ) { $dependencies = array(); foreach ( $deps as $dependency ) { if ( is_array( $dependency ) ) { if ( ! isset( $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' ); }Changelog
Introduced in 6.5.0. 14 changes between 6.7.10 and 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
7.0.7
Method
set_translations() added.verified against source7.0.7
Method
print_script_module_translations() added.verified against source7.0.7
Method
get_registered() added.verified against source6.9.10
Method
get_queue() added.verified against source6.9.10
Method
is_valid_fetchpriority() added.verified against source6.9.10
Method
set_fetchpriority() added.verified against source6.9.10
Method
set_in_footer() added.verified against source6.9.10
Method
get_highest_fetchpriority() added.verified against source6.9.10
Method
print_head_enqueued_script_modules() added.verified against source6.9.10
Method
print_script_module() added.verified against source6.9.10
Method
get_dependents() added.verified against source6.9.10
Method
get_recursive_dependents() added.verified against source6.9.10
Method
get_sorted_dependencies() added.verified against source6.9.10
Method
sort_item_dependencies() added.verified against source6.5.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.10 tag, from
src/wp-includes/class-wp-script-modules.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.