[wp-trac] [WordPress Trac] #60850: Localize script not working in WordPress 6.5-RC3-57856

WordPress Trac noreply at wordpress.org
Wed Mar 27 08:54:22 UTC 2024


#60850: Localize script not working in WordPress 6.5-RC3-57856
---------------------------+------------------------------
 Reporter:  dev28          |       Owner:  (none)
     Type:  defect (bug)   |      Status:  assigned
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  Script Loader  |     Version:  trunk
 Severity:  normal         |  Resolution:
 Keywords:  close          |     Focuses:
---------------------------+------------------------------
Changes (by swissspidy):

 * keywords:  has-patch => close
 * focuses:  javascript, php-compatibility =>
 * component:  Plugins => Script Loader
 * severity:  major => normal


Comment:

 Hi there and welcome to WordPress Trac!

 `wp_localize_script` works only if the script has already been registered.
 This has always been this way.

 In your [https://github.com/worldweb/loan-calculator-wp plugin] I see that
 you are registering the script on `wp_enqueue_script` and calling
 `wp_localize_script` in the shortcode callback.

 In classic theme that would work, because the former usually runs in
 `wp_head` and the shortcode is only parsed later.

 However, in block themes, the content (and shortcodes) can be parsed
 ''before'' the `wp_enqueue_scripts`, which leads to this scenario here
 where your script is not registered yet. This has always been this way.

 Since you are using `'loan-calculator-frontend-script'` only in the
 frontend anyway, an easy fix would be to move the script registration to
 the shortcode callback as well.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60850#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list