Posted by on December 27, 2018 at 3:20 pm in Web Development, WordPress
Updated: January 9, 2019 at 5:12 pm
Featured image

It is a recommended practice to link all your JavaScript files towards the bottom of your web page, just before the closing </body> tag.

To do this in WordPress, just insert the following code anywhere in your theme’s functions.php file.

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

This will cause all scripts and stylesheets to be moved from the header to the footer area.

To move just scripts from the header to the footer, use the following code instead:

remove_action('wp_head', 'wp_print_head_scripts', 9);
add_action('wp_footer', 'wp_print_head_scripts', 5);

Moving scripts to the footer is a good way to speed up your website’s loading time. Note that moving stylesheets to the footer can cause some weird behaviour, like your website looking like an 80’s txt document before styling is applied.

Tags: footer, scripts, wordpress

Related Posts

  • Featured Image
    WordPress – Showing cookie consent in comments
    WordPress has a neat feature that allows visitors to save their details in the browser when commenting. This prevents repeated typing fatigue. However, this option might not show up under the comments – even if the option is checked in the Admin settings page.
  • WordPress – Fixing issues with comment form not moving
    Sometimes, you might notice the comment form not moving underneath a comment as expected. I had a similar bug in one of my themes. I used the same functions and structures in two themes, but one had the comment form working properly, whereas in the other the comment form wasn’t moving. After spending hours trying […]
  • jQuery – Fixing HTML anchor tags with fixed navbars
    You may have noticed that your HTML # anchor tags no longer line up properly if you have a fixed navbar. It’s easily fixable with some simple jQuery.
  • Featured Image
    SSH command cheatsheet
    I find myself frequently using SSH to perform commands that otherwise take painfully long over regular FTP. I keep the following cheatsheet nearby to help me out.
  • Featured Image
    Bootstrap – Dealing with ‘File to import not found or unreadable’
    When importing Bootstrap files into your own sass, you might encounter this pesky error that might leave you scratching your head.