I am trying to load JQuery hosted on Google in my blog’s footer just before the body finishes. This is the function I am using in my functions.php file.
function load_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
// load the Google API copy in the footer
wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', array(), '1.5.1', 1);
wp_enqueue_script('jquery');
}
}
add_action('init', 'load_jquery');
However it keeps being loaded in the header. I can’t figure out why.
I also added the following function in the functions.php file and it works fine;
function your_function() {
echo '<p>This is inserted at the bottom</p>';
}
add_action('wp_footer', 'your_function');
Appreciate the help.
I suspect that one of the other scripts that is dependent on jquery is not set to load in the footer. Since the script is dependent on jquery, jquery will be loaded in the head.
This works for me
There is flag in
wp_enqueue_script
function:wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
You can specify that if you want to load the script in footer. take a look at the documentation: http://codex.wordpress.org/Function_Reference/wp_enqueue_script
First, you should use
As noted by Unknown_Guy.
Secondly, to load it only for pages that NEED the jQuery (non-admin pages in your case) then move the wp_enqueue_script(‘jquery’) line to a function that is only run when rendering user pages.
In our plugins we only call wp_enqueue_script(‘jquery’) in the function that renders the shortcode. That way jQuery is only loaded when shortcode rendering happens, which is when we need it. This will not load on admin pages or pages where our shortcodes are not in use.
There may be an undefined function that getting triggered before
wp_footer()
function.For my situation:
I have buddypress functions in my template files but I have deactivated buddypress plugin so functions were not working. To solve problem I added
if function exists
before all buddypress functions.Like so; Before:
After:
I did adding
function exists
for all undefined (even for all) functions.your mistake is that in the last parameter of
wp_register_script()
you are passing 1.If you read the documentation on wordpress the last parameter is a boolean, so you should pass either
true
orfalse
and not 1 or 0