#63401 closed defect (bug) (duplicate)
wp_localize_script() doesn't work with wp_register_script_module()
| Reported by: | mondalaci | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Script Loader | Version: | 6.8 |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
Consider the following code fragment:
wp_register_script('dummy', plugin_dir_url(__DIR__) . 'js/dummy.js', ['wp-util', 'jquery']);
wp_localize_script('dummy', 'search', [
'action' => 'search_knowledgebase',
'url' => admin_url('admin-ajax.php'),
'isAdmin' => current_user_can('administrator') || current_user_can('editor'),
'firstName' => wp_get_current_user()->first_name,
'email' => wp_get_current_user()->user_email,
]);
wp_enqueue_script('dummy');
As expected, the following gets injected on the page:
<script type="text/javascript" id="dummy-js-extra">
/* <![CDATA[ */
var search = {"action":"search_knowledgebase","url":"http:\/\/localhost\/wordpress\/wp-admin\/admin-ajax.php","isAdmin":"1","firstName":"L\u00e1szl\u00f3","email":"laci@monda.hu"};
/* ]]> */
</script>
However, when wp_register_script_module() is used instead of wp_register_script(), the above content is not injected on the page. I'd expect it to be injected.
Change History (2)
#1
@
17 months ago
- Component General → Script Loader
- Milestone Awaiting Review
- Resolution → duplicate
- Status new → closed
#2
@
12 months ago
In order to pass data to modules, a filter was introduced in WordPress 6.7:
https://make.wordpress.org/core/2024/10/14/updates-to-script-modules-in-6-7/#New-data-passing-API
Note:
See TracTickets
for help on using tickets.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
This is expected, because scripts ≠ modules.
I18N for modules is still missing and is tracked in #60234.