Skip to content

Instantly share code, notes, and snippets.

@zeropointdevelopment
Last active January 1, 2016 12:09
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 zeropointdevelopment/8142553 to your computer and use it in GitHub Desktop.
Save zeropointdevelopment/8142553 to your computer and use it in GitHub Desktop.
[WordPress] Code from our blog post - Passing Variables between PHP and jQuery - http://www.limecanvas.com/passing-variables-between-php-and-jquery/
// Load our jQuery script
function lc_load_jquery(){
wp_enqueue_script('lcjquerytest', get_stylesheet_directory_uri() . '/js/lc-jquery.js', array('jquery'), '1.0', true);
global $post;
$data = array( 'somestring' => 'pooky', 'post_id' => $post->ID, 'post_title' => $post->post_title )
wp_localize_script( 'lcjquerytest', 'lc_jqpost_info', $data );
}
add_action( 'wp_enqueue_scripts', 'lc_load_jquery' );
// Load our jQuery script
function lc_load_jquery(){
wp_enqueue_script('lcjquerytest', get_stylesheet_directory_uri() . '/js/lc-jquery.js', array('jquery'), '1.0', true);
}
add_action( 'wp_enqueue_scripts', 'lc_load_jquery' );
jQuery("#clickme").click(function () {
var msg = 'somestring='+lc_jqpost_info.somestring;
msg += ' post_id='+lc_jqpost_info.post_id;
msg += ' post_title='+lc_jqpost_info.post_title;
alert( msg );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment