[wp-trac] [WordPress Trac] #52534: PHP 8: wp_localize_script() throws a warning if third parameter is a string.

WordPress Trac noreply at wordpress.org
Tue Feb 16 04:24:13 UTC 2021


#52534: PHP 8: wp_localize_script() throws a warning if third parameter is a
string.
---------------------------+------------------------------
 Reporter:  peterwilsoncc  |       Owner:  (none)
     Type:  defect (bug)   |      Status:  new
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  Script Loader  |     Version:
 Severity:  normal         |  Resolution:
 Keywords:  php8           |     Focuses:
---------------------------+------------------------------
Description changed by peterwilsoncc:

Old description:

> Calling `wp_localize_script()` will throw a warning in PHP 8.0 if the
> third parameter (localization object) is a string:
>
> {{{#!php
> <?php
> wp_localize_script( 'jquery', 'jqueryL10n', __( 'Some string' ) );
> }}}
>
> PHP 8.0 Warning:
>

> {{{
> [16-Feb-2021 03:02:09 UTC] PHP Warning:  Only the first byte will be
> assigned to the string offset in /vagrant/wordpress-develop/src/wp-
> includes/class.wp-scripts.php on line 492
> [16-Feb-2021 03:02:09 UTC] PHP Stack trace:
> [16-Feb-2021 03:02:09 UTC] PHP   1. {main}() /vagrant/wordpress-
> develop/src/wp-admin/admin-ajax.php:0
> [16-Feb-2021 03:02:09 UTC] PHP   2. require_once() /vagrant/wordpress-
> develop/src/wp-admin/admin-ajax.php:22
> [16-Feb-2021 03:02:09 UTC] PHP   3. require_once() /vagrant/wordpress-
> develop/src/wp-load.php:37
> [16-Feb-2021 03:02:09 UTC] PHP   4. include() /vagrant/wordpress-
> develop/src/wp-config.php:9
> [16-Feb-2021 03:02:09 UTC] PHP   5. require_once() /vagrant/wp-
> config.php:152
> [16-Feb-2021 03:02:09 UTC] PHP   6. do_action($tag = 'init') /vagrant
> /wordpress-develop/src/wp-settings.php:560
> [16-Feb-2021 03:02:09 UTC] PHP   7. WP_Hook->do_action($args = [0 => ''])
> /vagrant/wordpress-develop/src/wp-includes/plugin.php:484
> [16-Feb-2021 03:02:09 UTC] PHP   8. WP_Hook->apply_filters($value = '',
> $args = [0 => '']) /vagrant/wordpress-develop/src/wp-includes/class-wp-
> hook.php:316
> [16-Feb-2021 03:02:09 UTC] PHP   9. PWCC\{closure:/vagrant/content/mu-
> plugins/pwcc-cpts.php:25-27}('') /vagrant/wordpress-develop/src/wp-
> includes/class-wp-hook.php:292
> [16-Feb-2021 03:02:09 UTC] PHP  10. wp_localize_script($handle =
> 'jquery', $object_name = 'jqueryL10n', $l10n = 'Some string')
> /vagrant/content/mu-plugins/pwcc-cpts.php:26
> [16-Feb-2021 03:02:09 UTC] PHP  11. WP_Scripts->localize($handle =
> 'jquery', $object_name = 'jqueryL10n', $l10n = 'Some string') /vagrant
> /wordpress-develop/src/wp-includes/functions.wp-scripts.php:221
> }}}
>
> ---
>
> `wp_localize_script()` is known to work with the following formats:
>
> * objects
> * associative arrays
> * indexed arrays
> * multidimensional arrays
> * strings
> * integers, floats (cast to a string as discussed in #29722 and #25280)
>

> Follow up to #29722

New description:

 Calling `wp_localize_script()` will throw a warning in PHP 8.0 if the
 third parameter (localization object) is a string:

 {{{#!php
 <?php
 wp_localize_script( 'jquery', 'jqueryL10n', __( 'Some string' ) );
 }}}

 PHP 8.0 Warning:


 {{{
 [16-Feb-2021 03:02:09 UTC] PHP Warning:  Only the first byte will be
 assigned to the string offset in /vagrant/wordpress-develop/src/wp-
 includes/class.wp-scripts.php on line 492
 [16-Feb-2021 03:02:09 UTC] PHP Stack trace:
 [16-Feb-2021 03:02:09 UTC] PHP   1. {main}() /vagrant/wordpress-
 develop/src/wp-admin/admin-ajax.php:0
 [16-Feb-2021 03:02:09 UTC] PHP   2. require_once() /vagrant/wordpress-
 develop/src/wp-admin/admin-ajax.php:22
 [16-Feb-2021 03:02:09 UTC] PHP   3. require_once() /vagrant/wordpress-
 develop/src/wp-load.php:37
 [16-Feb-2021 03:02:09 UTC] PHP   4. include() /vagrant/wordpress-
 develop/src/wp-config.php:9
 [16-Feb-2021 03:02:09 UTC] PHP   5. require_once() /vagrant/wp-
 config.php:152
 [16-Feb-2021 03:02:09 UTC] PHP   6. do_action($tag = 'init') /vagrant
 /wordpress-develop/src/wp-settings.php:560
 [16-Feb-2021 03:02:09 UTC] PHP   7. WP_Hook->do_action($args = [0 => ''])
 /vagrant/wordpress-develop/src/wp-includes/plugin.php:484
 [16-Feb-2021 03:02:09 UTC] PHP   8. WP_Hook->apply_filters($value = '',
 $args = [0 => '']) /vagrant/wordpress-develop/src/wp-includes/class-wp-
 hook.php:316
 [16-Feb-2021 03:02:09 UTC] PHP   9. PWCC\{closure:/vagrant/content/mu-
 plugins/pwcc-cpts.php:25-27}('') /vagrant/wordpress-develop/src/wp-
 includes/class-wp-hook.php:292
 [16-Feb-2021 03:02:09 UTC] PHP  10. wp_localize_script($handle = 'jquery',
 $object_name = 'jqueryL10n', $l10n = 'Some string') /vagrant/content/mu-
 plugins/pwcc-cpts.php:26
 [16-Feb-2021 03:02:09 UTC] PHP  11. WP_Scripts->localize($handle =
 'jquery', $object_name = 'jqueryL10n', $l10n = 'Some string') /vagrant
 /wordpress-develop/src/wp-includes/functions.wp-scripts.php:221
 }}}

 ---

 `wp_localize_script()` is known to work with the following formats:

 * associative arrays
 * indexed arrays
 * multidimensional arrays
 * strings



 Follow up to #29722

--

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


More information about the wp-trac mailing list