While trying to optimize my site loading time, I noticed a big pack of js in the head. After searching, I found a solution:

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' );

It works fine, all right, but... there are some plugins that include some inline scripts in the body, for example sliders, or another one. So scripts like jquery.js and jquery-migrate.min.js by this function moves to footer, and inline body jQuery text causes many errors.

Is there any way to rewrite the function to exclude some scripts from loading in the footer, like jquery.js and jquery-migrate.min.js?


You are removing the hooks from the head that are normally used to register and enqueue scripts. Normally, but not necessarily. You can also skip the intermediate hook and enqueue scripts directly into wp_head like this:


function wpse240186_enqueue_jquery() {
  wp_register_script('jquery', 'path_to/jquery.min.js');

In that way you can move scripts in general to the footer, like you have done, but place selected ones in the header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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