Blog

WordPress Asset Enqueuing

The way JavaScript and CSS are enqueued in WordPress can significantly affect how a site loads in the browser. Using a  CDN to host assets, loading JavaScript files in the footer, and combining/minifying CSS & JS using SASS or Less can dramatically speed up a site.

Many of our client sites don’t have the luxury of a CDN with their hosting, so we are always looking for ways to delegate the hosting of assets to reliable third party CDNs like Google CDN or CDNJS in order to boost load times. The great thing about CDNJS is that they host a large variety of assets that you won’t find on Google CDN, and it’s easy to find minified versions of a current library and serve those assets using the SPDY protocol. If you don’t find a library in CDNJS, you can always submit a library yourself via GitHub. There is even a CDNJS Plugin for WordPress.

The code block below deregisters/enqueues a local theme stylesheet, jQuery, jQuery Migrate, and a local theme JavaScript file (and conditionally enqueues the built-in comment-reply script as well). jQuery Migrate is a useful plugin if you are including plugins that rely on older versions of jQuery. The code fragment can be placed within your functions.php and the $styles and $scripts array should be modified to reference all of the assets you want integrated with your theme.

Use the YSlow and PageSpeed Insights browser extensions to analyze the performance of your site and to provide suggestions to make things faster. WebPageTest is another great tool for checking your site’s performance.