Grow your CSS skills. Land your dream job.

WordPress and jQuery

  • # July 8, 2013 at 9:38 am

    I’m enqueuing a copy of jQuery into my theme. Is it best to load jQuery in the head or the footer?

    On static sites I always go for footer but for plugin compatibility etc is it best to go with the head?

    # July 8, 2013 at 9:45 am

    Load library in the header, and your custom functions in the footer.

    # July 8, 2013 at 11:33 am

    Cheers man!

    Is that due to plugins etc using the wp-head hook to insert their scripts

    # July 8, 2013 at 11:33 am

    Also, am I best just using the version of jQuery that comes with WP?

    # July 8, 2013 at 11:51 am

    Your custom functions should come as late as possible, to make sure that the library and all HTML has loaded. It’s not just for WordPress, you should always do this with any site.

    There’s various ways of loading the library: your own local one, the WP one, a CDN one…all have their pros and cons. I personally always use the one that comes with WP but that’s only because I’m lazy. :)

    # July 8, 2013 at 1:24 pm

    I realise this that’s why I personally always load jquery just before the close of the body but I thought doing this with WP could be risky if plugins that rely on jquery were being loaded into the head I.e before jquery.

    So my final question do you load jquery in the head or before the close of the body?

    Cheers

    # July 8, 2013 at 2:02 pm

    > Your custom functions should come as late as possible, to make sure that the library and all HTML has loaded. It’s not just for WordPress, you should always do this with any site. @Senff

    I always load libraries at the bottom. The only thing that gets loaded in the `` in my projects is Modernizr. Everything else (jQuery, plugins, site.js, etc) get loading just before the closing ``.

    # July 8, 2013 at 2:13 pm

    I too prefer @TheDoc approach. I use this technique to load jQuery at the bottom:
    http://beneverard.co.uk/blog/wordpress-loading-jquery-correctly-version-2/

    Then I brute force any plugins that register scripts in the head to move to the footer, below jQuery:

    http://www.kevinleary.net/faster-wordpress-move-javascript-files-footer/

    # July 9, 2013 at 8:43 am

    Thanks guys, I must admit I didn’t like loading scripts in the head just seems plain wrong.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".