[wp-trac] [WordPress Trac] #54777: passing empty object to wp_localize_script will convert it to empty array instead of empty object

WordPress Trac noreply at wordpress.org
Mon Jan 10 10:44:36 UTC 2022


#54777: passing empty object to wp_localize_script will convert it to empty array
instead of empty object
--------------------------+-----------------------------
 Reporter:  erikdemarco   |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 This will correctly converted to js object:

 {{{
 wp_localize_script( $handle, $object_name, array(

     "foo" => "Foo value",
     "bar" => "Bar value"

 ) );
 }}}

 the value will be:

 {{{
 {"foo":"Foo value","bar":"Bar value"}
 }}}



 But passing empty array will be converted to js empty array:

 {{{
 wp_localize_script( $handle, $object_name, array() );
 }}}

 the value will be:

 {{{
 []
 }}}


 We want to pass empty array because we want to prepare the global js
 variable to be used later. But because of the inconsistency it can cause
 problem if we want to loop it for later use. since we use different method
 to loop object and array in javascript.

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


More information about the wp-trac mailing list