How to Move JavaScripts to the Bottom or Footer in WordPress

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 js directory. 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:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5

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

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3

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.

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:

Tables can't be imported directly. Please insert an image of your table which can be found here.


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. 

Was this article helpful?
0 out of 0 found this helpful