[wp-trac] [WordPress Trac] #26111: wp_localize_script array from callback for performance

WordPress Trac noreply at wordpress.org
Tue Nov 19 14:41:31 UTC 2013


#26111: wp_localize_script array from callback for performance
-------------------------+-----------------------------
 Reporter:  ciantic      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Performance  |    Version:  trunk
 Severity:  normal       |   Keywords:
-------------------------+-----------------------------
 These days `wp_localize_script` is being used to shovel all kinds of PHP
 -> JavaScript data, not just localization, and the performance may become
 bottleneck. It's recommended to write localize scripts during enequeue
 scripts action but this is still run every time.

 It would be useful for performance to be able to put callback  parameter
 which in turn would return the array, e.g. for my imaginary date_i18n
 script:

 {{{
 wp_localize_script("date_i18n", "DATE_I18N", null, function () {
         global $wp_locale;
         $monthNames = array_map(array(&$wp_locale, 'get_month'), range(1,
 12));
         $monthNamesShort = array_map(array(&$wp_locale,
 'get_month_abbrev'), $monthNames);
         $dayNames = array_map(array(&$wp_locale, 'get_weekday'), range(0,
 6));
         $dayNamesShort = array_map(array(&$wp_locale,
 'get_weekday_abbrev'), $dayNames);

         return array(
                 "month_names" => $monthNames,
                 "month_names_short" => $monthNamesShort,
                 "day_names" => $dayNames,
                 "day_names_short" => $dayNamesShort
         );
 });
 }}}

 WordPress could then run the callback ''only if the script e.g. date_18n
 is enqueued''.

 For backwards compatibility it is required to introduce fourth parameter
 the `$l10n_callback` for the `wp_localize_script`, this is why in my
 example third is `null`.

 I must also note, that since wp_localize_script is being used to pass data
 to javascript (e.g. retrieving posts from db) one could also introduce a
 new function like `wp_data_script`, but this would not be backwards
 compatible.

 Thank you for considering.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/26111>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list