wp_register_script not loading as expected

I am loading scripts like this:

//load scripts
//wp_register_script( $handle, $src, $deps, $ver, $in_footer );

add_action('wp_enqueue_scripts', 'load_scripts');

function load_scripts() {

    if (!is_admin()) {

        wp_register_script('modernizr', get_bloginfo('template_directory') .'/scripts/modernizr-latest.js',false);
        wp_enqueue_script('modernizr');

        wp_enqueue_script('jquery', true );

        wp_register_script('cookie',get_bloginfo('template_directory') . '/scripts/cookie.js', array('jquery'), '1.0',true);
        wp_enqueue_script('cookie');

        wp_register_script( 'Gmaps', 'http://maps.google.com/maps/api/js?sensor=false', true );
        wp_enqueue_script ('Gmaps');

        wp_register_script('plugins', get_bloginfo('template_directory') .'/scripts/plugins.js',true);
        wp_enqueue_script('plugins');

        wp_register_script( 'maps_scripts',  get_bloginfo('template_directory') . '/scripts/maps.js', array('Gmaps'), '1.0', true );
        wp_enqueue_script ('maps_scripts');

        wp_register_script('history', get_bloginfo('template_directory') .'/scripts/history.js', array('jquery'), '1.0',true);
        wp_enqueue_script('history');


    }
}

With this I expect to see all scripts except modernizr loaded into the footer. Instead it is an incorrect mish-mash. From this example jquery, google maps, and the plugins.js still load in the header even though $in_footer is set to true. Cookie, maps, and history load in the footer as expected.

Read More

Can someone please help explain this to me?

Related posts

Leave a Reply

1 comment

  1. Just a note before I get into the solution, get_template_directory_uri() is preferred to get_bloginfo() because it is a filtered output. That said, in the corrected code I have left it the way you had it, to save you typing in the case that you had a reason for leaving it that way.

    Your issue is that you’re neglecting some parameters, the parameter before the parenthesis is not the last parameter, it always counts from the left, so in a lot of these, you have set $ver to true rather than the $in_footer to true. Also, you should not need to enqueue jquery, that should be done automatically, but there’s no harm in doing it anyways.

    add_action( 'wp_enqueue_scripts', 'load_scripts ');
    
    function load_scripts() {
    
        if ( !is_admin() ) {
    
            wp_enqueue_script( 'jquery' );
    
            wp_register_script( 'modernizr', get_bloginfo( 'template_directory' ) .'/scripts/modernizr-latest.js' );
            wp_enqueue_script( 'modernizr' );
    
            wp_register_script( 'cookie', get_bloginfo( 'template_directory' ) . '/scripts/cookie.js', array( 'jquery' ), '1.0', true );
            wp_enqueue_script( 'cookie' );
    
            wp_register_script( 'Gmaps', 'http://maps.google.com/maps/api/js?sensor=false', array(), false, true );
            wp_enqueue_script ( 'Gmaps' );
    
            wp_register_script( 'plugins', get_bloginfo( 'template_directory' ) .'/scripts/plugins.js', array(), false, true );
            wp_enqueue_script( 'plugins' );
    
            wp_register_script( 'maps_scripts',  get_bloginfo( 'template_directory' ) . '/scripts/maps.js', array( 'Gmaps' ), '1.0', true );
            wp_enqueue_script ( 'maps_scripts' );
    
            wp_register_script( 'history', get_bloginfo( 'template_directory' ) .'/scripts/history.js', array( 'jquery' ), '1.0', true );
            wp_enqueue_script( 'history' );
    
    
        }
    }
    

    Just a note: this might be worth a read, it helps to get everything uniform. Personally I try to write everything to the coding standard, even if it doesn’t have to be, as a habit of good practice.