waynedunkley
3/10/2015 - 1:28 AM

Localize script - Make server-side data accessible to local scripts

Localize script - Make server-side data accessible to local scripts

// 
function enqueue_scripts() {
  
  // 1. Clientside scripts must first be enqueued,
  // 2. then make data localized and attach to local scripts
  
  wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0.0', true );
  
  wp_localize_script( 'script', 'local_vars', array(
    'images_dir'  => get_stylesheet_directory_uri() . '/images/',
    'security' => wp_create_nonce( 'my-special-string' )
  ));

}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );