WP_Script_Modules::add_hooks
Adds the hooks to print the import map, enqueued script modules and script module preloads.
In classic themes, the script modules used by the blocks are not yet known when the wp_head is fired, so it needs to print everything in the footer.
Method of the class: WP_Script_Modules{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WP_Script_Modules = new WP_Script_Modules(); $WP_Script_Modules->add_hooks();
Changelog
| Since 6.5.0 | Introduced. |
WP_Script_Modules::add_hooks() WP Script Modules::add hooks code WP 7.0.2
public function add_hooks() {
$is_block_theme = wp_is_block_theme();
$position = $is_block_theme ? 'wp_head' : 'wp_footer';
add_action( $position, array( $this, 'print_import_map' ) );
if ( $is_block_theme ) {
/*
* Modules can only be printed in the head for block themes because only with
* block themes will import map be fully populated by modules discovered by
* rendering the block template. In classic themes, modules are enqueued during
* template rendering, thus the import map must be printed in the footer,
* followed by all enqueued modules.
*/
add_action( 'wp_head', array( $this, 'print_head_enqueued_script_modules' ) );
}
add_action( 'wp_footer', 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' ), 9 );
add_action( 'admin_print_footer_scripts', array( $this, 'print_enqueued_script_modules' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_preloads' ) );
/*
* Print translations after classic scripts like wp-i18n are loaded (at
* priority 10 via _wp_footer_scripts), but before the script modules
* execute. Script modules with type="module" are deferred by default,
* so inline translation scripts at priority 11 will execute before them.
*/
add_action( 'wp_footer', array( $this, 'print_script_module_translations' ), 21 );
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_translations' ), 11 );
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 );
}