Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
The Enqueue function takes a final boolean parameter for this, true if you want it to be in the footer:
http://codex.ww.wp.xz.cn/Function_Reference/wp_enqueue_script
Would you be able to write a small example for this? I’m new to jQuery/WP integration.
Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
What is the code you’re using to enqueue the script at the moment?
function my_init() {
if (!is_admin()) {
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’, ‘http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js’, false, ‘1.10.2’, true);
wp_enqueue_script(‘jquery’);
// My Custom Script
wp_enqueue_script(‘scripts’, get_bloginfo(‘template_url’) . ‘/javascripts/scripts.js’, array(‘jquery’), ‘1.0’, true);
}
}
add_action(‘init’, ‘my_init’);
Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
Oh, you shouldn’t be deregistering jQuery. It can cause all sorts of problems in the future with the compatibility of your theme with the core WordPress application.
Regarding your enqueue function you are already putting the “scripts.js” file at the bottom of your page just before the “</body>” tag by setting the last bit (that I’ve highlighted in bold) as “true”:
wp_enqueue_script(‘scripts’, get_bloginfo(‘template_url’) . ‘/javascripts/scripts.js’, array(‘jquery’), ‘1.0’, true);
1) Should I remove the 3rd line, ‘wp_deregister_script(‘jquery’);’
2) So in a sense by setting wp_enqueue_script I’m appending the code to the bottom of my page(s)? If so that makes so much more sense to me and thank you so much for clearing that up!
3) My last question would be for a lot of these plugins they have their initializer and then a completely separate .js file. Should I just include the initializer in that .js file or is there something I’m still not understanding?
Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
1) Replace all of your function with this:
function my_init() {
// My Custom Script
wp_enqueue_script( 'scripts', get_bloginfo( 'template_url' ) . '/javascripts/scripts.js', array( 'jquery' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_init' );
Worked out perfectly! Thank you. Any suggestions for #3?
Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
You can just put that inside your scripts.js file.