[wp-trac] [WordPress Trac] #60862: wp_localize_script() on login_enqueue_scripts hook change in behavior

WordPress Trac noreply at wordpress.org
Sun Apr 7 12:38:48 UTC 2024


#60862: wp_localize_script() on login_enqueue_scripts hook change in behavior
----------------------------------------+---------------------
 Reporter:  salcode                     |       Owner:  (none)
     Type:  defect (bug)                |      Status:  new
 Priority:  normal                      |   Milestone:  6.5.1
Component:  Login and Registration      |     Version:  6.5
 Severity:  normal                      |  Resolution:
 Keywords:  needs-unit-tests has-patch  |     Focuses:
----------------------------------------+---------------------

Comment (by GuerillaDesigns):

 I have a similar issue where wp_localize_script used to work in a block
 registration, but since WP 6.5 that localization isn't happening at time
 where the data is usable by the JS. Because I can easily get that data in
 another way I have just worked around it. But I'm curious if this is
 related or not.

 {{{
 function moneygauge_cta_block_init() {
     $script_handle = 'moneygauge-cta-block-editor';
     wp_register_script(
         $script_handle,
         plugins_url('build/index.js', __FILE__),
         array('wp-blocks', 'wp-editor'),
         filemtime(plugin_dir_path(__FILE__) . 'build/index.js')
     );

     // Assuming your image is located at <your-plugin-
 dir>/assets/handsup.jpg
     $image_url = plugins_url('assets/handsup.jpeg', __FILE__);

     wp_localize_script($script_handle, 'moneygaugeData', [
         'imageUrl' => $image_url, // This is the full URL to the image
     ]);

     register_block_type(__DIR__ . '/build', [
         'editor_script' => $script_handle,
     ]);
 }
 add_action('init', 'moneygauge_cta_block_init');
 }}}

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


More information about the wp-trac mailing list