Codex tools: Log in
<?php wp_localize_script( $handle, $name, $data ); ?>
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.
<script> tag containing your localization variable occurs at the time that the enqueued script is printed (output/included on the page). This has some significant repercussions if you enqueue your script as you should using the appropriate actions (wp_enqueue_scripts and admin_enqueue_scripts), but wish to localize later using data that is not available at enqueue time.
In this case, consider enqueueing your script with the in_footer argument set to true, to delay the printing of your script include until much later in the page build (ie:
wp_enqueue_script( $slug, $URL, $deps, $ver, true ); ).
<?php wp_enqueue_script( 'some_handle' ); $translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' ); wp_localize_script( 'some_handle', 'object_name', $translation_array ); ?>
<script> alert(object_name.some_string); // alerts 'Some string to translate' </script>
<script> FinalZoom = map.getBoundsZoomLevel(bounds)-parseInt(data.a_value,10); // Call a function that needs an int. </script>
wp_localize_script() is located in