Add the wp_script_module_attributes filter.#10324
Add the wp_script_module_attributes filter.#10324samueljseay wants to merge 1 commit intoWordPress:trunkfrom
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.
Can't the existing filter be used? I don't think we really need a new filter.
There was a problem hiding this comment.
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.
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.
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.
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.
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.