WordPress GuideDevelopment → Combine External JavaScript

How to combine external JavaScript in WordPress for better performance

Image

JavaScript makes WordPress sites more interactive, but too much of it—especially from multiple external sources—can slow your site down fast. If your site is taking longer to load or performing poorly on speed tests, combining external JavaScript files could give you a major performance boost.

Let’s walk through what it means to combine external JavaScript, why it helps, and how to safely do it with or without plugins.

Get fast, reliable hosting for WordPress

Power your site with the industry’s fastest, most optimized WordPress hosting

Why combine external JavaScript files in WordPress?

Every time a browser loads your WordPress site, it has to download each JavaScript file listed in your theme or plugins. When there are lots of separate files—especially from external sources like social media widgets, analytics tools, or plugins—it creates multiple HTTP requests. Each one of those requests adds load time.

Combining JavaScript files reduces the number of these requests, which can lead to:

But keep in mind, not all scripts should be combined. Some are best left separate, especially those that need to load in a specific order or that come from third-party sources you don’t control.

Before you start: backup and benchmark your site

Before you touch any files or install optimization plugins, take a few minutes to protect and evaluate your site.

Option 1: combine JavaScript using a WordPress plugin

If you’re not comfortable writing code or editing theme files, plugins are your best bet. They offer a safer and more beginner-friendly way to combine and optimize scripts.

Recommended plugin options

Each of these handles JavaScript combining a little differently, but they’re all designed to improve performance:

For most users, Autoptimize is the easiest to get started with. Let’s walk through how to use it.

How to combine JS with Autoptimize

Option 2: combine JavaScript manually via functions.php

If you’re comfortable editing theme files and want more control, you can manually combine scripts yourself. This method is more flexible but also riskier, so take it slow and test frequently.

Step-by-step: combine external JS manually

Async and defer: control how JavaScript loads

Even after combining your JS files, your site can still benefit from controlling when and how scripts are loaded.

To apply defer to your combined script, add this to functions.php:

This ensures your script doesn’t block the browser from rendering the page.

You can also use the Async JavaScript plugin (by the same developer as Autoptimize) for easier control of async and defer attributes without code.

Common issues and how to fix them

Combining scripts can cause problems if you’re not careful. Here’s what to watch out for:

If something goes wrong, you can always disable the plugin or restore from your backup.

Tips for maintaining performance

Once you’ve combined your scripts and optimized performance, keep things running smoothly by:

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

Additional resources

What is managed WordPress hosting? →

Get details and decide if managed WordPress hosting is right for you.

How to install WordPress with Nginx →

Learn how to install and configure WordPress with NGINX for improved performance and flexibility.












A complete guide to WordPress shortcodes →

Shortcodes make life easier. Learn how to get started!

Trust us to help you choose the ideal hosting solution

Loading form…