Filter hook 'script_module_data_{$module_id}'

in WP Core File wp-includes/class-wp-script-modules.php at line 443

View Source

script_module_data_{$module_id}

Filter Hook
Description
Filters data associated with a given Script Module. Script Modules may require data that is required for initialization or is essential to have immediately available on page load. These are suitable use cases for this data. The dynamic portion of the hook name, `$module_id`, refers to the Script Module ID that the data is associated with. This is best suited to pass essential data that must be available to the module for initialization or immediately on page load. It does not replace the REST API or fetching data from the client. Example: add_filter( 'script_module_data_MyScriptModuleID', function ( array $data ): array { $data['dataForClient'] = 'ok'; return $data; } ); If the filter returns no data (an empty array), nothing will be embedded in the page. The data for a given Script Module, if provided, will be JSON serialized in a script tag with an ID of the form `wp-script-module-data-{$module_id}`. The data can be read on the client with a pattern like this: Example: const dataContainer = document.getElementById( 'wp-script-module-data-MyScriptModuleID' ); let data = {}; if ( dataContainer ) { try { data = JSON.parse( dataContainer.textContent ); } catch {} } // data.dataForClient === 'ok'; initMyScriptModuleWithData( data );

Hook Information

File Location wp-includes/class-wp-script-modules.php View on GitHub
Hook Type Filter
Line Number 443

Hook Parameters

Type Name Description
array $data The data associated with the Script Module.

Usage Examples

Basic Usage
<?php
// Hook into script_module_data_{$module_id}
add_filter('script_module_data_{$module_id}', 'my_custom_filter', 10, 1);

function my_custom_filter($data) {
    // Your custom filtering logic here
    return $data;
}

Source Code Context

wp-includes/class-wp-script-modules.php:443 - How this hook is used in WordPress core
<?php
 438  			 *
 439  			 * @since 6.7.0
 440  			 *
 441  			 * @param array $data The data associated with the Script Module.
 442  			 */
 443  			$data = apply_filters( "script_module_data_{$module_id}", array() );
 444  
 445  			if ( is_array( $data ) && array() !== $data ) {
 446  				/*
 447  				 * This data will be printed as JSON inside a script tag like this:
 448  				 *   <script type="application/json"></script>

PHP Documentation

<?php
/**
			 * Filters data associated with a given Script Module.
			 *
			 * Script Modules may require data that is required for initialization or is essential
			 * to have immediately available on page load. These are suitable use cases for
			 * this data.
			 *
			 * The dynamic portion of the hook name, `$module_id`, refers to the Script Module ID
			 * that the data is associated with.
			 *
			 * This is best suited to pass essential data that must be available to the module for
			 * initialization or immediately on page load. It does not replace the REST API or
			 * fetching data from the client.
			 *
			 * Example:
			 *
			 *     add_filter(
			 *         'script_module_data_MyScriptModuleID',
			 *         function ( array $data ): array {
			 *             $data['dataForClient'] = 'ok';
			 *             return $data;
			 *         }
			 *     );
			 *
			 * If the filter returns no data (an empty array), nothing will be embedded in the page.
			 *
			 * The data for a given Script Module, if provided, will be JSON serialized in a script
			 * tag with an ID of the form `wp-script-module-data-{$module_id}`.
			 *
			 * The data can be read on the client with a pattern like this:
			 *
			 * Example:
			 *
			 *     const dataContainer = document.getElementById( 'wp-script-module-data-MyScriptModuleID' );
			 *     let data = {};
			 *     if ( dataContainer ) {
			 *         try {
			 *             data = JSON.parse( dataContainer.textContent );
			 *         } catch {}
			 *     }
			 *     // data.dataForClient === 'ok';
			 *     initMyScriptModuleWithData( data );
			 *
			 * @since 6.7.0
			 *
			 * @param array $data The data associated with the Script Module.
			 */
Quick Info
  • Hook Type: Filter
  • Parameters: 1
  • File: wp-includes/class-wp-script-modules.php
Related Hooks

Related hooks will be displayed here in future updates.