lundi 29 décembre 2014

Using wp_localize_script to store the template url into a variable to use in JS


I was trying to store the template url (bloginfo(template_url);) into a variable so I could use it in my JavaScript. However, I realized this wasn't possible and someone alerted me to wp_localize_script and said that it could do what I wanted. I looked through the codex but I'm still unsure as to how to implement this.


Here's what I have so far:


Function



function starter_scripts() {

wp_enqueue_script( 'jquery' );

wp_localize_script( 'my_script', 'templateUrl', array(
'templateUrl' => template_url()
) );
}
add_action( 'wp_enqueue_scripts', 'starter_scripts' );


JS



jQuery(document).ready(function($){

// Fade in Contact background
$('body.page-template-page-contact #content').css('background', 'url(templateUrl + bg-contact.jpg) 50% 0% no-repeat fixed').fadeIn(2000);

});


Where have I gone wrong?





Aucun commentaire:

Enregistrer un commentaire