-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add the wp_script_module_attributes filter. #10324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the wp_script_module_attributes filter. #10324
Conversation
| * @param string $id The script module identifier. | ||
| * @param array $script_module The script module data. | ||
| */ | ||
| $args = apply_filters( 'wp_script_module_attributes', $args, $id, $script_module ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't the existing filter be used? I don't think we really need a new filter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't that have unexpected consequences? Someone who is using the wp_script_attributes filter today only expects to be modifying the scripts, not the script modules.
For example, if someone is using that filter to add defer to some scripts with a conditional that is also true for some script modules, it would generate invalid HTML for them:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because as you see below, it is using wp_print_script_tag(). This function is used for printing any script, including classic scripts, script modules, speculationrules. This function calls wp_get_script_tag() and that function is what applies the wp_script_attributes filters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhh, it's already there. We didn't notice that!
Thanks, Weston. Closing this then, as it's not needed. We'll update GB as well 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've drafted a PR as a starting point: WordPress/gutenberg#72449
I'm guessing lib/compat/wordpress-6.9/script-modules.php can be removed as well.
| * @param string $id The script module identifier. | ||
| * @param array $script_module The script module data. | ||
| */ | ||
| $args = apply_filters( 'wp_script_module_attributes', $args, $id, $script_module ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd make the arguments match the existing filter for scripts (wp_script_attributes):
- Rename
$argsto$attributes - Remove
$id, which can be obtained from the$attributes['id']anyway - Eliminate
$script_module, whose schema is not publicly exposed for now, and this exposure would mean that backward compatibility would have to be maintained from now on.
| $args = apply_filters( 'wp_script_module_attributes', $args, $id, $script_module ); | |
| $args = apply_filters( 'wp_script_module_attributes', $attributes ); |
|
Closing as the attributes of the script modules can already be filtered using |
Trac ticket: https://core.trac.wordpress.org/ticket/64111
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.