Skip to content

Commit

Permalink
Docs: Correct formatting for script module data filter documentation …
Browse files Browse the repository at this point in the history
…examples.

Follow-up to [58579].

Props jonsurrell.
See #62281.

git-svn-id: https://develop.svn.wordpress.org/trunk@59505 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Dec 11, 2024
1 parent 32880ec commit 9104190
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,15 @@ public function print_script_module_data(): void {
* 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['script-needs-this-data'] = 'ok';
* return $data;
* }
* );
* 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.
*
Expand All @@ -423,15 +424,17 @@ public function print_script_module_data(): void {
*
* 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 {}
* }
* initMyScriptModuleWithData( data );
* 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
*
Expand Down

0 comments on commit 9104190

Please sign in to comment.