[wp-trac] [WordPress Trac] #46334: wp_localize_script did not output anything in wp_print_footer_scripts hook although document suggested that it should be working

WordPress Trac noreply at wordpress.org
Sun Feb 24 01:10:11 UTC 2019


#46334: wp_localize_script did not output anything in wp_print_footer_scripts hook
although document suggested that it should be working
---------------------------+-----------------------------
 Reporter:  simongcc       |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Script Loader  |    Version:  4.9.8
 Severity:  major          |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 Note: it should be WordPress 4.9.9 but the version list did not provide
 this option

 According to this page, there is a statement showing that the "localize"
 can be achieved at the last chance hook "wp_print_footer_scripts"
 https://codex.wordpress.org/Function_Reference/wp_localize_script

   The last chance to localize your script would then be on the
 'wp_print_footer_scripts' hook.

 However, I have tried the following code, it did not showing up. I tried
 to trace back the original localize() function and even looking into the
 WP_Dependencies class, I only find that it is added to the queue for
 localize() but it is gone at print_extra_script() stage. Cannot find out
 the exact reason, maybe due to the enqueue process and timing is only
 allowed to be used in the "wp_enqueue_script" hook.

 The following is working.
 {{{#!php
 <?php
 function theme_enqueue_scripts() {
     wp_enqueue_script( 'handle_name_js', SOMEWHERE . '/js/custom.js',
 array( 'jquery' ), false, true);
     wp_localize_script( 'handle_name_js', 'memberMeta', array('member' =>
 'list') );
 }
 add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts', 12 );
 }}}

 Since my page have a variable appear almost end of the page so I need to
 localize it in later time and considered "wp_print_footer_scripts" hook,
 so I tried like this:
 Adding the handle first and localize it in later time.

 {{{#!php
 <?php
 function theme_enqueue_scripts() {
     wp_enqueue_script( 'handle_name_js', SOMEWHERE . '/js/custom.js',
 array( 'jquery' ), false, true);
 }
 add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts', 12 );

 function set_member_meta() {
     global $team_member_meta;
     // var_dump() something here, proved that this function is called and
 got the variable
     // where $team_member_meta is as simple as even array('1234') or
 array('member' => 'list')

     // tried also putting here but not working as well, the handle is also
 being queued but gone in output
     // wp_enqueue_script( 'handle_name_js', SOMEWHERE . '/js/custom.js',
 array( 'jquery' ), false, true);
     // did not output anything
     wp_localize_script( 'handle_name_js', 'memberMeta', $team_member_meta
 );
 }
 add_action( 'wp_print_footer_scripts', 'set_member_meta', 20 );
 }}}

 I have traced through core, the handle data is added actually.
 But it is out of my ability to trace the exact point of problem because it
 is pointing from a to b and b to c with many checking or modifiers, I am
 not familiar enough with the core nor have enough debugging technique in
 dealing with wordpress hooks.

 I run the above code inside functions or functions required php files in
 theme folder.
 Because it is not working as expected nor as suggested in the document and
 the trace of the code is also showing that the handle is finally gone
 before using it. So I think it is possibly a bug.

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


More information about the wp-trac mailing list