Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Scripts To Footer Plugin

January 21, 2014 by Joshua Nelson

stfI released my first official plugin about a year ago now: Scripts to Footer. It’s a very light-weight plugin (a single function, actually) that removes a few hooks controlling the script placement in WordPress. This results in those scripts being placed at the bottom of the page, instead of the top.

Why do that, you may wonder? Well, browsers load from the top of your page to the bottom, and sometimes those pesky scripts can take a little time to load. So, while you’re user is waiting for your page to load the browser is busy downloading jQuery. Instead, if you place your scripts at the bottom of the page (which should be standard practice!) it will load your page first and the scripts second. The difference in load times can be substantial.

There are a lot of plugins out there that take steps to improve page load times, but I couldn’t find one that was simple and straight-forward. This plugin takes a broad-stroke approach and forces all your scripts into footer – but only if they are enqueued properly! It has come in handy on quite a few sites I’ve created and I’ve seen significant decrease in page load times in a most cases.

Conflicts and Excluding Files (Keep Some Files in the Head)

I’ve also notice that is doesn’t always work with AJAX-enabled Gravity Forms or any scripted elements that require in-page scripting (some scripts need to be loaded in the header before the in-page script in order to work). There are a few workarounds in those cases – turn the plugin off and try something else, for example. Or, you can determine the name of the specific script and use the following code to exclude it – or, more correctly, to place it back into the head.

<?php
/**
* Exclude scripts from Scripts to Footer plugin
* http://wordpress.org/plugins/scripts-to-footerphp/
*
* In this example, we will use jQuery (hosted on Google's servers even)
*/
add_action( 'wp_print_styles', 'jdn_deregister_scripts', 100 );
add_action( 'wp_head', 'jdn_head_scripts' );
function jdn_deregister_scripts() {
wp_deregister_script( 'jquery' ); // The slug or name used to enqueue the script
}
function jdn_head_scripts() { // The html for the script that will be placed in the header
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<?php
}
view raw exclude-scripts.php hosted with ❤ by GitHub

Disable Plugin on Specific Pages

Update from 2-17-14: Some users have requested that the plugin be disabled on specific pages where they were having conflicts. It seems the biggest area of concern is different slider plugins, which usually only appear on one or two pages.

Update from 2-23-14 I’ve added a checkbox option to disable the plugin on specific pages/posts as of version 0.2.

More Information

Check out the plugin page. Download it yourself and take it for a spin!

You can also view this plugin on GitHub here.

Filed Under: Plugins Tagged With: javascript, performance

  • Twitter
  • RSS Feed URL

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.

See my services and my recent work.

Contact me to get your project started.

Gravity Forms Plugin for WordPress Fastest WordPress Hosting

Recent Posts

  • Using Font Awesome Icons for WooCommerce Grid / List Toggle
  • Disable Blog: WordPress Gone Blog-less
  • Category (Taxonomy) Dropdown Filtered By Post Type
  • Weather in WordPress with Dark Sky
  • Fixing Your Deprecated Widget Constructors in WordPress 4.3
  • Twitter
  • RSS Feed URL
  • Code Snippets
  • My Plugins
  • Make a Payment

© Joshua David Nelson | Hand-Forged | WordPress + Genesis | Terms of Service | Legal | Contact