0

I'm trying to move jquery to my wordpress footer-page (to optimize pagespeed and keep google happy), but can't quite figure out how to do it.

It looks like jquery.js + 3 other js.files are located in scripts.php (through "wp_enqueue_script"), which in turn is referenced in functions.php.

I'm somewhat comfortable editing wordpress php & css files, but do not know how to write php actually and feel I need a little help so I won't break my site.

How and where do I move javascript so it's loaded last?

1
  • I suggest just above the closing </body> tag and if you have more javascript includes there let jQuery be the first one due to dependancies Commented Nov 15, 2015 at 14:23

2 Answers 2

2

wp_enqueue_script can place a script in the footer. You will need to "deregister" it and then re-register and enqueue:

add_action("wp_enqueue_scripts", function()
{
    wp_deregister_script('jquery');
    wp_register_script('jquery', "path_to_jquery", false, 'whatever', true);
    wp_enqueue_script("jquery");
});

The last argument passed as true places the script in the footer. The code above should be placed in functions.php.

Sign up to request clarification or add additional context in comments.

1 Comment

Doing this actually moves jquery in the footer, but it loads after all the other scripts of the theme. So nothing works fine because it results that jquery is not loaded. Any idea how to load jquery in the footer but before all the other scripts of the theme ?
0

Thanks for your help! Although I did try the code, in the end I found a wordpress plugin that seems to do the trick called "scripts-to-footer" - works like a charm so far.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.