How to Move JavaScripts to the Bottom or Footer in WordPress

By | June 16, 2014

Recently, one of our readers asked us how they can move JavaScripts to the bottom in WordPress to increase their Google page speed score. We are glad they asked, because honestly we wanted to write about this. Previously, we have talked about how to properly add JavaScripts and CSS styles in WordPress. In this article, we will show you how to move JavaScripts to the bottom in WordPress, so you can improve your site load time and your Google page speed score.

Benefits of Moving JavaScripts to the Bottom

JavaScript is a client side programming language. It is executed and run by a user’s web browser and not by your web server. When you put JavaScript at the top, browsers may execute or process the JavaScript before loading the rest of your page. When JavaScripts are moved to the bottom, your web server would quickly render the page and then the user’s browser would execute JavaScripts. Since all the server side rendering is already done, the JavaScript will load in the background making the overall load faster.

This will improve your speed score when testing with Google page speed or Yslow. Google and other search engines are now considering page speed as one of the performance matrices when displaying search results. This means that websites that load faster will appear more prominently in search results.

The Proper Way of Adding Scripts in WordPress

WordPress has a powerful enqueuing system which allows theme and plugin developers to add their scripts in queue and load them as needed. Enqueuing scripts and styles properly can significantly improve your page load speed.

To show you a basic example, we will add a little JavaScript into a WordPress theme. Save your JavaScript in a .js file and place that .js file in your theme’s jsdirectory. If your theme does not have a directory for JavaScripts, then create one. After placing your script file, edit your theme’s functions.php file and add this code:

function wpb_adding_scripts() {
wp_register_script('my-amazing-script', get_template_directory_uri() . '/js/my-amazing-script.js','','1.1', true);
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

In this code, we have used wp_register_script() function. This function has the following parameters:

wp_register_script( $handle, $src, $deps, $ver, $in_footer );

To add the script in the footer or bottom of a WordPress page, all you need to do is set the $in_footer parameter to true.

We have also used another function get_template_directory_uri() which returns the URL for the template directory. This function should be used for enqueuing and registering scripts and styles in WordPress themes. For plugins we will be using plugins_url() function.

The Problem:

The problem is that some times WordPress plugins add their own JavaScript to pages inside <head> or inside page body. In order to move those scripts to the bottom you need to edit your plugin files and properly move the scripts to the bottom.

Finding The JavaScript Source

Open your site in the web browser and view page source. You will see the link to the JavaScript file indicating the location and the origin of the file. For example, the screenshot below tells us that our script belongs to a plugin called ‘test-plugin101′. The script file is located in the js directory.

Finding source of a script in WordPress

Sometimes you will see JavaScript added directly into the page and not linked through a separate .js file. In that case, you would need to deactivate all your plugins one by one. Refresh the page after deactivating each plugin until you find the one adding the script to your pages. If the JavaScript does not disappear even after deactivating all the plugins, then try to switch to another theme to see if the JavaScript is added by your theme.

Register and Enqueue Scripts

Once you have found the plugin or theme that is adding JavaScript in the header section, the next step is to find out where the plugin has a call for the file. In one of your theme or plugin’s PHP files you will see a call to that particular .js file.

If the plugin or theme is already using enqueuing to add JavaScript file, then all you need to do is change wp_register_script function in your plugin or theme and add true for the $in_footer parameter. Like this:

wp_register_script('script-handle', plugins_url('js/script.js'  , __FILE__ ),'','1.0',true);

Lets assume that your plugin or theme is adding raw JavaScript in the header or between the content. Find the raw JavaScript code in the plugin or theme files, copy the JavaScript and save it in a .js file. Then use wp_register_script() function as shown above, to move JavaScript to the bottom.

Editor’s note: It is important to understand that when you make changes to the core files and update the plugin, then your changes will not be overridden. A better way of doing this would be to deregister the script and re-register it from your theme’s functions.php file. See this tutorial.

Aside from moving the scripts to the footer, you should also consider using a fastersocial media plugin and lazy load images. Along with that you should also use W3 Total Cache and MaxCDN to improve your site speed.

We hope that this article helped you move JavaScripts to the bottom in WordPress and improve your page speed. For questions and feedback please leave a comment below.


Leave a Reply

Your email address will not be published. Required fields are marked *