Skip to content

Instantly share code, notes, and snippets.

@kostasx
Created January 3, 2019 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kostasx/b0d36b18625ec45c1a555e4fdc32bef7 to your computer and use it in GitHub Desktop.
Save kostasx/b0d36b18625ec45c1a555e4fdc32bef7 to your computer and use it in GitHub Desktop.
WordPress Localized Scripts: Pass PHP data to JavaScript - The PHP code.
<?php
// Place this code in your child theme's functions.php file
function myprefix_enqueue_scripts() {
wp_enqueue_script(
'custom-script',
get_stylesheet_directory_uri() . '/js/custom.js'
);
wp_localize_script( 'custom-script', 'php_data', array(
'message' => __('A message that can be translated!' )
)
);
}
add_action('wp_enqueue_scripts', 'myprefix_enqueue_scripts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment