WP_Script_Modules::add_hooks()

Adds the hooks to print the import map, enqueued script modules and script module preloads.


Description

It adds the actions to print the enqueued script modules and script module preloads to both wp_head and wp_footer because in classic themes, the script modules used by the theme and plugins will likely be able to be printed in the head, but the ones used by the blocks will need to be enqueued in the footer.

As all script modules are deferred and dependencies are handled by the browser, the order of the script modules is not important, but it’s still better to print the ones that are available when the wp_head is rendered, so the browser starts downloading those as soon as possible.

The import map is also printed in the footer to be able to include the dependencies of all the script modules, including the ones printed in the footer.


Source

File: wp-includes/class-wp-script-modules.php

	public function add_hooks() {
		add_action( 'wp_head', array( $this, 'print_enqueued_script_modules' ) );
		add_action( 'wp_head', array( $this, 'print_script_module_preloads' ) );
		add_action( 'wp_footer', array( $this, 'print_enqueued_script_modules' ) );
		add_action( 'wp_footer', array( $this, 'print_script_module_preloads' ) );
		add_action( 'wp_footer', array( $this, 'print_import_map' ) );
	}


Changelog

Changelog
Version Description
6.5.0 Introduced.