W3cubDocs

/WordPress

WP_Script_Modules::add_hooks()

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

Description

In classic themes, the script modules used by the blocks are not yet known when the wp_head actions is fired, so it needs to print everything in the footer.

Source

public function add_hooks() {
	$position = wp_is_block_theme() ? 'wp_head' : 'wp_footer';
	add_action( $position, array( $this, 'print_import_map' ) );
	add_action( $position, array( $this, 'print_enqueued_script_modules' ) );
	add_action( $position, array( $this, 'print_script_module_preloads' ) );

	add_action( 'admin_print_footer_scripts', array( $this, 'print_import_map' ) );
	add_action( 'admin_print_footer_scripts', array( $this, 'print_enqueued_script_modules' ) );
	add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_preloads' ) );

	add_action( 'wp_footer', array( $this, 'print_script_module_data' ) );
	add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_data' ) );
	add_action( 'wp_footer', array( $this, 'print_a11y_script_module_html' ), 20 );
	add_action( 'admin_print_footer_scripts', array( $this, 'print_a11y_script_module_html' ), 20 );
}

Changelog

Version Description
6.5.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_script_modules/add_hooks