Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wish to move jQuery to the bottom of the page on my theme, to speed up loading times.

Previously I was doing this by replacing it with Google's CDN version but I have been told that is a bad thing to do.

How can I deregister and re-register the WordPress jQuery script and move it to the bottom without causing any problems?

share|improve this question
1  
Why? Using Google’s jQuery will break many plugins, because it works differently. –  toscho Mar 12 '13 at 9:39
    
Here is a post why loading jQuery from a CDN isn't a ´good´ idea. pippinsplugins.com/why-loading-your-own-jquery-is-irresponsible –  Horttcore Mar 12 '13 at 9:40
    
OK I did not know about the problems with using Google's CDN. I will instead just move it to the footer. Thanks. –  Shaun Mar 12 '13 at 9:42
    
This definitely has causing issue. I've received some complaints about this. It turns out optimize press theme is doing this...... –  Jesse Mar 12 '13 at 9:47
    
OK guys I changed the question as I'm still not sure how to do this. –  Shaun Mar 12 '13 at 9:47

1 Answer 1

You can change it by calling add_data method of $wp_scripts object. This object holds all scripts and information how to render it. To force rendering script in the footer you can do it like this:

add_action( 'wp_enqueue_scripts', 'wpse8170_enqueue_scripts' );
function wpse8170_enqueue_scripts() {
    $GLOBALS["wp_scripts"]->add_data( 'jquery', 'group', 1 );
}

P.S.: I haven't tested it, but suppose it has to work.

share|improve this answer
    
Just tried this, it caused the following error: Fatal error: Call to a member function add_data() on a non-object –  Shaun Mar 12 '13 at 10:00
    
The latest edit by toscho stops the error coming up but also stops jQuery loading at all. –  Shaun Mar 12 '13 at 11:20
    
@Shaun yes, sorry, forgot to call global $wp_scripts before adding data. –  Eugene Manuilov Mar 12 '13 at 11:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.