Skip to content

Instantly share code, notes, and snippets.

@Phoenix2k
Last active December 10, 2016 22:39
Show Gist options
  • Save Phoenix2k/89d6fc8a8bd198891646 to your computer and use it in GitHub Desktop.
Save Phoenix2k/89d6fc8a8bd198891646 to your computer and use it in GitHub Desktop.
WordPress: Method for passing localized strings via wp_localize_script() to an external .js file
<?php
/**
* Localize strings for JavaScripts
*
* @link https://codex.wordpress.org/Function_Reference/wp_localize_script
*/
// Enqueue the script which will use the localized strings
wp_enqueue_script( 'name-of-your-script', '/js/yourscript.js' );
// Define an array of data to be passed on to your JavaScript file
wp_localize_script( 'name-of-your-script', 'objectL10n', array(
'localized_string' => __( 'Localized string', 'theme-name' ),
));
// objectL10n will be passed on to this file
console.log( objectL10n.localized_string );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment