Skip to content

Script Loader: Resolve the global script modules instance in hook callbacks - #13509

Open
sirreal wants to merge 4 commits into
WordPress:trunkfrom
sirreal:fix/script-modules-hooks-use-global
Open

sirreal wants to merge 4 commits into
WordPress:trunkfrom
sirreal:fix/script-modules-hooks-use-global

Conversation

@sirreal

@sirreal sirreal commented Sep 14, 2026

Copy link
Copy Markdown
Member

WP_Script_Modules::add_hooks() registers array( $this, 'method' ), so the print callbacks are bound to the instance that was the global on after_setup_theme. Replacing $wp_script_modules later changes what enqueues but not what prints. Scripts and styles use procedural callbacks that read the global at run time.

This adds procedural print functions to script-modules.php and registers those instead. Hook names, priorities, and the instance methods are unchanged.

Test: register the hooks, replace the global, enqueue a module, fire admin_print_footer_scripts. Fails on trunk with empty output.

Compatibility: remove_action() with the instance callback no longer matches. AMP does this in includes/class-amp-theme-support.php.

Trac ticket: https://core.trac.wordpress.org/ticket/66100

Follow-up to r57503.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Fable 5.1, Claude Opus
Used for: code, test, description.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@sirreal
sirreal force-pushed the fix/script-modules-hooks-use-global branch from 8bc861d to 6a90a3c Compare September 14, 2026 07:03
…lbacks.

`WP_Script_Modules::add_hooks()` registered every print callback as
`array( $this, 'method' )`, binding the callbacks to the instance that
was the global when `wp-settings.php` ran `add_hooks()` on
`after_setup_theme`. Replacing the `$wp_script_modules` global after that
point changed where `wp_enqueue_script_module()` wrote, but not what was
printed. Classic scripts and styles do not have this problem because
their hooks are procedural functions that read the global when they run.

Add procedural print functions that delegate to `wp_script_modules()` and
register those instead.

See #66100, #64484.
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
@sirreal
sirreal marked this pull request as ready for review September 14, 2026 07:43
@github-actions

github-actions Bot commented Sep 14, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

…unctions.

Five of the seven new procedural print functions declared no return
type while `wp_print_script_module_translations()` and
`wp_print_script_module_data()` declared `: void`. Declare `: void` on
the other five so the set is consistent. None of them returns a value.

See #66100.

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have concerns about this:

Compatibility: remove_action() with the instance callback no longer matches. AMP does this in includes/class-amp-theme-support.php.

In addition to the AMP plugin removing these, this is also done by 3 other plugins, with atum-stock-manager-for-woocommerce (10K+ active installs) seeming being the most concerning: https://veloria.dev/search/5536fd24-0f42-484b-8b24-813e03b952be

In searching GitHub, I also see this would seem to break some Jetpack code: https://github.com/Automattic/jetpack/blob/fc6b865fe09ea7b407925c7e66757f7dd7c4dcf7/projects/packages/jetpack-mu-wpcom/src/features/wpcom-blocks/code/class-code-block.php#L587-L634

This also would break some old Gutenberg compat code which is no longer shipping: https://github.com/WordPress/gutenberg/blob/497e846fe0f157daf97912d95c860558de495574/lib/compat/wordpress-6.7/script-modules.php#L92-L104

It would also break this plugin from PEW Research: https://github.com/pewresearch/prc-print-engine/blob/8f927a2af9b991987c5990ee5cabaaa1062e3dac/includes/class-print-engine.php#L589-L612

I fear the back-compat breakage could be too great.


While not related to the compat question, these lines of code in Gutenberg would eventually need to be updated as well:

// Print import map first so it's available for inline scripts
wp_script_modules()->print_import_map();
print_footer_scripts();
wp_script_modules()->print_enqueued_script_modules();
wp_script_modules()->print_script_module_preloads();
wp_script_modules()->print_script_module_data();

These will continue to work as-is in the mean time.

@sirreal

sirreal commented Sep 16, 2026

Copy link
Copy Markdown
Member Author

This was motivated by #13507 originally. That work no longer depends on this change so I don't have a pressing need to land this.

However, this is a correctness issue. The change in r57503 does not have the desired impact because if the

I do share the back-compat concerns, but I'm also worried that this is a bug where the global can be modified but that doesn't actually do anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants