Getting notice is wordpress "WP_Scripts::localize was called incorrectly"

Viewed 13261

Complete message: Notice: WP_Scripts::localize was called incorrectly. The $l10n parameter must be an array. To pass arbitrary data to scripts, use the wp_add_inline_script() function instead. Please see Debugging in WordPress for more information. (This message was added in version 5.7.0.) in /home3/dduconne/public_html/wp-includes/functions.php on line 5313

Appeared just after updating wordpress to 5.7.0

4 Answers

Ok since there quite a few views and the previous answer just throws it under the rug.

Yes it is a warning now, but who knows what will happen, it is a warning for a reason, disabling something is not resolving it, there is a reason devs set it as a warning for now.

While we wait for plugin developers to resolve it on their ends, you can find out the source of the problem by enabling php_xdebug in php. I recommend not leaving it after debugging, as I am not sure about the performance cost of it being enabled.

php_xdebug will return a stack of all files affected, from there you can trace it to the source of the problem. The fix once found the source is quite easy. Culprit most likely is wp_localize_script() which requires last parameter to be an array()!

So you would find something like this:

wp_localize_script( 'handle', 'object_name', 'l10n_culprit_string' );

Which should be turned into:

wp_localize_script( 'handle', 'object_name', array( 'l10n_culprit_string' ) );

Solution to problem came from here

This is a new warning appearing in Wordpress 5.7. If you don't want to see it, and still want to have WP_DEBUG set to true, it is possible to disable the message by adding the following for example in your theme's functions.php:

 add_filter('doing_it_wrong_trigger_error', function () {return false;}, 10, 0);

It is now a notice if you are using wp5.7 and php7.4, but changing to php8 it may gets reported as error. For me, when i changed (in my own code) the passed value to array (yes, probably some changes also needed in the involved js), everything worked just fine.

This is because one of the PHP script in your wordpress installation uses wp_localize_script() to pass some PHP variables to a JS script through AJAX request, and it's not good practice anymore. This can be inside a plugin or inside your custom theme or child-theme's functions.php. If wp_localize_script() is not in a child-theme or custom theme's functions.php, try this :

  1. backup your website
  2. deactivate all plugins
  3. reactivate them one by one until you get the notice again
  4. when the notice shows up again, this means the plugin you just reactivated uses wp_localize_script() the wrong way
  5. if it's not up-to-date, try updating it (if you want to) : maybe the notice is going to disappear
  6. if it's already up-to-date, or you don't want to update it, search inside wp-content/plugins the corresponding plugin's folder
  7. open it and try to find the file(s) where wp_localize_script() is called, you can also use CLI with your server's terminal to find string in files. For example, if your server runs on Linux : grep -H -r "wp_localize_script()" /root/pathtopluginfolder | cut -d: -f1
  8. Then, replace the code as follow :

Old one should look almost like this :

wp_enqueue_script( 'wpdocs-my-script', 'https://url-to/my-script.js' );
wp_localize_script( 'wpdocs-my-script', 'MYSCRIPT', array(
    'ajaxUrl' => admin_url( 'admin-ajax.php' ),
    'otherParam' => 'some value',
) );

New one :

wp_enqueue_script( 'wpdocs-my-script', 'https://url-to/my-script.js' );
wp_add_inline_script( 'wpdocs-my-script', 'const MYSCRIPT = ' . json_encode( array(
    'ajaxUrl' => admin_url( 'admin-ajax.php' ),
    'otherParam' => 'some value',
) ), 'before' );

Note that you need to add 'before' as the third parameter to the wp_add_inline_script function.

Once it's done, the notice shouldn't show anymore. If so, contact the plugin dev to tell him about this so that it can be part of the next update.

For more informations, see Jules Colle's comment on this page : https://developer.wordpress.org/reference/functions/wp_add_inline_script/

Related