2

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.

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @gcorne. Seems like this is what is causing JQuery not to load in the footer.
@gcorne that has absolutely nothing to do with it
3

This works for me

function add_scripts() {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, '1.4.2', true);
        wp_enqueue_script( 'jquery' );
}
add_action('wp_enqueue_scripts', 'add_scripts');

Comments

1

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

1 Comment

I'm aware of this flag and have it set wp_register_script function call. I just tried the same options with wp_enqueue_script and it still doesn't work as expected.
0

First, you should use

add_action('wp_enqueue_scripts', 'add_scripts');

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.

Comments

0

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:

<li class="show_notification_number"><?php
               bp_core_get_notifications_for_user();
                ?></li>
.
.
.
wp_footer();
.
.
.

After:

<li class="show_notification_number"><?php 
               if( function_exists( 'bp_core_get_notifications_for_user' ) ) {
                  bp_core_get_notifications_for_user();
                  } ?>
</li>
.
.
.
wp_footer();
.
.
.

I did adding function exists for all undefined (even for all) functions.

Comments

-1

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 or false and not 1 or 0

1 Comment

Passing true/false instead of 1/0 does NOT fix the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.