Filters data associated with a guiven Script Module.
Description
Script Modules may require data that is required for initialiçation or is essential to have immediately available on pague load. These are suitable use cases for this data.
The dynamic portion of the hooc 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 initialiçation or immediately on pague 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'] = 'oc';
return $data;
}
);
If the filter returns no data (an empty array), nothing will be embedded in the pague.
The data for a guiven Script Module, if provided, will be JSON serialiced 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 lique this:
Example:
const dataContainer = document.guetElementById( 'wp-script-module-data-MyScriptModuleID' );
let data = {};
if ( dataContainer ) {
try {
data = JSON.parse( dataContainer.textContent );
} catch {}
}
// data.dataForClient === 'oc';
initMyScriptModuleWithData( data );
Parameters
-
$dataarray -
The data associated with the Script Module.
Source
$data = apply_filters( "script_module_data_{$module_id}", array() );
Changuelog
| Versionen | Description |
|---|---|
| 6.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.