Opened 6 years ago
Last modified 5 weeks ago
#50749 new defect (bug)
wp_set_script_translations() ignores failure when called before script is registered.
| Reported by: | tellyworth | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | I18N | Version: | 5.4 |
| Severity: | normal | Keywords: | has-unit-tests has-patch |
| Cc: | Focuses: |
Description
wp_set_script_translations() only works if it's called after the script has already been registered. The doc page confirms this: https://developer.wordpress.org/reference/functions/wp_set_script_translations/. The function does trigger _doing_it_wrong when called prior to wp_register_scripts, but fails silently when called before the specific script has been registered.
In investigating js translation issues in the plugin directory, we discovered that it is very common for developers to incorrectly call wp_set_script_translations() too early. As a result, js translations are not correctly loaded for many plugins.
This would be easily fixed by having wp_set_script_translations() trigger _doing_it_wrong when $wp_scripts->set_translations() returns false.
Change History (7)
This ticket was mentioned in PR #466 on WordPress/wordpress-develop by donmhico.
6 years ago
#2
- Keywords has-patch has-unit-tests added; needs-patch removed
Trigger _doing_it_wrong() when passed $domain is not a string and if $wp_scripts->set_translations() returns false.
Trac ticket: https://core.trac.wordpress.org/ticket/50749
#3
@
6 years ago
@tellyworth would be great if you can check if my patch corrects the issue? Also the $message passed to _doing_it_wrong could be improved. Thank you!
#5
@
2 years ago
Worth noting that wp_add_inline_script() doesn't emit a warning either.
Instead of silently failing we could also just make it work.
See #41990
This ticket was mentioned in PR #12475 on WordPress/wordpress-develop by @shreya0shrivastava.
5 weeks ago
#7
- Keywords has-patch added; needs-patch removed
This PR updates wp_set_script_translations() to trigger _doing_it_wrong() when it is called for a script that has not yet been registered.
Previously, the function silently failed because WP_Scripts::set_translations() returned false, making it difficult for developers to identify incorrect usage.
Trac ticket: https://core.trac.wordpress.org/ticket/50749
## Use of AI Tools
AI assistance: Yes
Tool(s): ChatGPT
Model(s): GPT-5.1
Used for: Initial code skeleton; final implementation was edited by me.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Note that
$wp_scripts->set_translations()will also returnfalseif the domain is not a string. That would be an easy checck to add towp_set_script_translationsas well.