I get a method not defined error when trying to call a function loaded with wp_enqueue_script
from site.js
.
I'm guessing it's because of scope caused by the jQuery ready wrappers? How do I get around that?
site.js (loaded in head
)
jQuery(document).ready(function($) {
$(window).load(function() { // wait until everything has loaded
doSomething(); // results in face palm
});
});
my-script.js (loaded in footer)
jQuery(document).ready(function($) {
function doSomething() {
alert("Time for a coffee break!");
}
});
functions.php
wp_register_script( 'my-script', get_template_directory_uri().'/js/my-script.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'my-script' );
// Pass in PHP variables
wp_localize_script( 'my-script', 'my_script', localize_post_vars() );