Last active
December 10, 2016 22:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ), | |
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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