[wp-trac] [WordPress Trac] #63401: wp_localize_script() doesn't work with wp_register_script_module()

WordPress Trac noreply at wordpress.org
Tue May 6 16:33:05 UTC 2025


#63401: wp_localize_script() doesn't work with wp_register_script_module()
--------------------------+-----------------------------
 Reporter:  mondalaci     |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  6.8
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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 at 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63401>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list