Details of wp_localize_script

wp_localize_scripts writes content to the page in the form of a Javascript object that can be accesses by other Javascript that runs on the page. The point is to be able to pass dynamic data to the executing Javascript without having to make AJAX requests, parse the page for data (extract the post ID from class attributes, for example), or hide data on the page is some hacked together way.

The first parameter is the slug of a registered and enqueued script. You have to “hook” or associate wp_localize_script with another script that has been registered and enqueued and you must run wp_localize_script after that other script has been registered and enqueued, not before. Sequence is important.

IMPORTANT! wp_localize_script() MUST be called after the script it’s
being attached to has been enqueued or registered. It doesn’t put the
localized script in a queue for later scripts.

http://codex.wordpress.org/Function_Reference/wp_localize_script

The second parameter is the name of the object that will printed to the page. This is the name that will be used to access the object data. You can think of it like a variable name except that you will use Javascript object syntax to access the data.

The third parameter is the actual data that the object will contain. It can be a single or a multi-dimensional array (multi as of 3.3).

All three parameters are required.

Leave a Comment