#61510 closed feature request (fixed)
Script Modules: Add mechanism to pass data from server to client
Reported by: | jonsurrell | Owned by: | jonsurrell |
---|---|---|---|
Milestone: | 6.7 | Priority: | normal |
Severity: | normal | Version: | 6.5 |
Component: | Script Loader | Keywords: | has-patch has-unit-tests needs-dev-note |
Focuses: | javascript | Cc: |
Description
One useful feature available to Scripts that Script Modules do not support is a way for the server to send data to the client with the page. Scripts typically do this with wp_add_inline_script
.
Proposal: Server to client data sharing for Script Modules proposes a mechanism for Script Modules to fill this need.
Part of #60647.
Change History (6)
This ticket was mentioned in PR #6682 on WordPress/wordpress-develop by @jonsurrell.
3 months ago
#2
- Keywords has-patch has-unit-tests added
@Bernhard Reiter commented on PR #6682:
3 months ago
#3
Committed to Core in https://core.trac.wordpress.org/changeset/58579.
Note: See
TracTickets for help on using
tickets.
Add the
print_script_module_data
function to theWP_Script_Modules
class.Register hooks to call this function on
wp_footer
andadmin_print_footer_scripts
.See the Make/Core Proposal and the PR in Gutenberg. In summary (from the proposal):
The new filter is
"script_module_data_{$module_id}"
. It should accept and return an array that will be serialized in page HTML (if it is not empty):add_filter( 'script_module_data_myModuleID', function ( array $data ): array { $data['some-data-here'] = 'the client can read this on page load'; return $data; } );
#6683 includes an example of usage in for Core Script Modules.
Closes #6433 (superseded).
Trac ticket: https://core.trac.wordpress.org/ticket/61510