WordPress wp_register_script function not loading script in footer

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.

Read More

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.

Related posts

Leave a Reply

6 comments

  1. 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.

  2. 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');
    
  3. 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.

  4. 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.

  5. 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