functions - how to en-queue jQuery to load before the <body> tag

admin2025-01-07  9

I've enqueued all my scripts on function.php to load before the tag by passing 'true' to the fifth argument. It does works for all of them BUT jquery, any idea what I'm doing wrong?

wp_enqueue_script('jquery','','','',true);
wp_enqueue_script('modernizr', '.8.2/modernizr.js', array('jquery'), false, true);
wp_enqueue_script('my-javascript', get_template_directory_uri() . '/js/my-javascript.js', array('jquery', 'modernizr'), false, true);
wp_enqueue_script('jquery-ui', get_template_directory_uri() . '/js/jquery-ui.min.js', array('jquery'), false, true);

I've enqueued all my scripts on function.php to load before the tag by passing 'true' to the fifth argument. It does works for all of them BUT jquery, any idea what I'm doing wrong?

wp_enqueue_script('jquery','','','',true);
wp_enqueue_script('modernizr', 'http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.2/modernizr.js', array('jquery'), false, true);
wp_enqueue_script('my-javascript', get_template_directory_uri() . '/js/my-javascript.js', array('jquery', 'modernizr'), false, true);
wp_enqueue_script('jquery-ui', get_template_directory_uri() . '/js/jquery-ui.min.js', array('jquery'), false, true);
Share Improve this question asked Nov 15, 2016 at 11:13 AnaAna 111 bronze badge 1
  • Would also like an answer to that. Have the same code for jQuery but it still loads after the head. – lowtechsun Commented Jan 11, 2017 at 0:13
Add a comment  | 

1 Answer 1

Reset to default 0

Try adding this to functions.php

<?php

// Custom scripting to move JavaScript from the head 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');

?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256272a348.html

最新回复(0)