Make WordPress Core


Ignore:
Timestamp:
12/11/2024 03:18:43 PM (22 months ago)
Author:
SergeyBiryukov
Message:

Docs: Correct formatting for script module data filter documentation examples.

Follow-up to [58579].

Props jonsurrell.
See #62281.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-script-modules.php

    r59089 r59505  
    408408                         * fetching data from the client.
    409409                         *
    410                          * @example
    411                          *   add_filter(
    412                          *     'script_module_data_MyScriptModuleID',
    413                          *     function ( array $data ): array {
    414                          *       $data['script-needs-this-data'] = 'ok';
    415                          *       return $data;
    416                          *     }
    417                          *   );
     410                         * Example:
     411                         *
     412                         *     add_filter(
     413                         *         'script_module_data_MyScriptModuleID',
     414                         *         function ( array $data ): array {
     415                         *             $data['dataForClient'] = 'ok';
     416                         *             return $data;
     417                         *         }
     418                         *     );
    418419                         *
    419420                         * If the filter returns no data (an empty array), nothing will be embedded in the page.
    … …  
    424425                         * The data can be read on the client with a pattern like this:
    425426                         *
    426                          * @example
    427                          *   const dataContainer = document.getElementById( 'wp-script-module-data-MyScriptModuleID' );
    428                          *   let data = {};
    429                          *   if ( dataContainer ) {
    430                          *     try {
    431                          *       data = JSON.parse( dataContainer.textContent );
    432                          *     } catch {}
    433                          *   }
    434                          *   initMyScriptModuleWithData( data );
     427                         * Example:
     428                         *
     429                         *     const dataContainer = document.getElementById( 'wp-script-module-data-MyScriptModuleID' );
     430                         *     let data = {};
     431                         *     if ( dataContainer ) {
     432                         *         try {
     433                         *             data = JSON.parse( dataContainer.textContent );
     434                         *         } catch {}
     435                         *     }
     436                         *     // data.dataForClient === 'ok';
     437                         *     initMyScriptModuleWithData( data );
    435438                         *
    436439                         * @since 6.7.0
Note: See TracChangeset for help on using the changeset viewer.