Reputation: 495
In this file
wp-include/script-loader.php
Have this code:
$scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.11.3');
$scripts->add( 'jquery-core', '/wp-includes/js/jquery/jquery.js', array(), '1.11.3');
$scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '1.2.1');
How do I put the jquery in the footer?
I've tried adding a fifth parameter "true" or "1", but does not work.
...
* Localizes some of them.
* args order: $scripts->add( 'handle', 'url', 'dependencies', 'query-string', 1 );
* when last arg === 1 queues the script for the footer
...
I want to put the jquery at the bottom because it is blocking the correct page load (google page speed recommendation).
Upvotes: 6
Views: 12544
Reputation: 315
After experiencing all these problems and testing workarounds myself without any joy, I decided to just migrate all scripts in one go.
So how about going one better and having some code that loads all your script files, present and future into the footer. Preventing this ballache from repeating. Also handy for any plugins you may add.
Open your functions.php file and add this bad boy
// Script to move all Head scripts to the Footer
function remove_head_scripts() {
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);
}
add_action( 'wp_enqueue_scripts', 'remove_head_scripts' );
// END of ball ache
Upvotes: 7
Reputation: 4244
You have to dequeue it first and then enqueue it again. The following code will do exactly what you need.
function jquery_mumbo_jumbo()
{
wp_dequeue_script('jquery');
wp_dequeue_script('jquery-core');
wp_dequeue_script('jquery-migrate');
wp_enqueue_script('jquery', false, array(), false, true);
wp_enqueue_script('jquery-core', false, array(), false, true);
wp_enqueue_script('jquery-migrate', false, array(), false, true);
}
add_action('wp_enqueue_scripts', 'jquery_mumbo_jumbo');
Upvotes: 9
Reputation: 923
Cf docs: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
You have a hook: wp_enqueue_scripts
Use:
function themeslug_enqueue_script() {
wp_enqueue_script( 'my-js', 'filename.js', false );
}
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );
Upvotes: -1