WordPress.org

Ready to get started?Download WordPress

Forums

Use Google Libraries
[resolved] Loading jQuery in footer (5 posts)

  1. jancbeck
    Member
    Posted 1 year ago #

    Is there a way to make UGL load the library scripts in the footer of the page? I'm using this code to call jQuery before my other scripts.

    function theme_ressources() {
        wp_enqueue_script('jquery');
        wp_enqueue_script('init', get_template_directory_uri() . '/js/init.js', array('jquery'), '1.0', true );
        // moar
    }
    add_action('wp_enqueue_scripts', 'theme_ressources');

    However this puts jQuery in the <head> of my page. I'd like to place all my scripts before the closing </body> Tag.
    What do I have to do to print jQuery and all following script to the bottom of my page?

    http://wordpress.org/extend/plugins/use-google-libraries/

  2. Jason Penney
    Member
    Plugin Author

    Posted 1 year ago #

    It's up to you where you want your script to be loaded (header/footer). It's up to WordPress core where jQuery is loaded, and currently it's required to be in the header. UGL does not let you change this because it would break WordPress.

  3. jancbeck
    Member
    Posted 1 year ago #

    But loading it in footer of my theme will neither break my theme nor WordPress' admin interface. From my perspective it makes sense to let me define where stuff is placed in my theme.
    However, this is probably a minor issue since we are talking about Google CDN that hopefully gets cached in the users browser anyway plus since its a different hostname parallel download limits should be no problem.

  4. Jason Penney
    Member
    Plugin Author

    Posted 1 year ago #

    That's outside the scope of what I want UGL doing. WordPress Core makes that decision, and UGL abides by it.

  5. jancbeck
    Member
    Posted 1 year ago #

    wpengineer just posted an article on how to solve this:

    <?php
    add_action( 'wp_default_scripts', 'ds_enqueue_jquery_in_footer' );
    /**
     * Plugin Name: Enqueue jQuery in Footer
     * Version:     0.0.1
     * Plugin URI:  http://wpgrafie.de/836/
     * Description: Prints jQuery in footer on front-end.
     * Author:      Dominik Schilling
     * Author URI:  http://wpgrafie.de/
     */
    function ds_enqueue_jquery_in_footer( &$scripts ) {
    
    	if ( ! is_admin() )
    		$scripts->add_data( 'jquery', 'group', 1 );
    }

    This changes the behavior of the default script registration so jQuery is loaded in footer. It goes without saying that you should test if this breaks any functionality for your plugins before using it in production.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic