[wp-trac] [WordPress Trac] #44003: multiple wp_localize_script not working

WordPress Trac noreply at wordpress.org
Tue May 8 07:51:50 UTC 2018


#44003: multiple wp_localize_script not working
---------------------------+-----------------------------
 Reporter:  gekomees       |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Script Loader  |    Version:  4.9.5
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 Only the latest wp_localize_script is supplied to the javascript files
 regardless of handle in wp_localize_script function

 I have two javascript enqueues and both have a wp_localize_script attached
 to them. However, only the latter wp_localize_script is actually applied
 to both of them.

 This is my code:

 {{{#!php
 <?php
     wp_register_script('filters', get_template_directory_uri() .
 '/theme/js/filters.js', false, '1.0.0', true);
     $localization = array(
         'ajax_url' => admin_url( 'admin-ajax.php' ),
         'language' => ICL_LANGUAGE_CODE
     );

     wp_localize_script( 'filters', 'phpvariables', $localization );
     wp_enqueue_script('filters');

         $themejspath_url = get_template_directory_uri() .
 '/theme/js/functions.js';
         $themejspath = get_template_directory() .
 '/theme/js/functions.js';
         wp_register_script('ama', $themejspath_url, false, filemtime(
 $themejspath ), true);
     $localization = array(
         'ajax_url' => admin_url( 'admin-ajax.php' ),
         'purchaser_name' => __('Maksja nimi', 'ama'),
         'purchaser_email' => __('Maksja e-mail', 'ama'),
         'booker_name' => __('Broneerija nimi', 'ama'),
         'booker_email' => __('Broneerija e-mail', 'ama'),
     );

     wp_localize_script( 'ama', 'phpvariables', $localization );
         wp_enqueue_script('ama');
 }}}

 It doesn't matter if wp_localize_script is on top or wp_enqueue_script.
 The filters.js should get the localized array with the language variable
 but it doesn't. It gets the second one with the translatable strings.
 This is the same even without WPML.

 Commenting out the wp_localize_script( 'ama', 'phpvariables',
 $localization ); line allows the filters.js to get the localization with
 the language variable. This is strange, as the handles are different and
 thus the handler filters should not get the localization for the handler
 ama

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


More information about the wp-trac mailing list