Estimated Reading Time: 3 min
How to Minify JavaScript in WordPress (Reduce Payload & Parse Time)
Minifying JavaScript removes unnecessary whitespace, comments, and redundant code, reducing file sizes and improving page load speed. Here’s how to do it efficiently:
1. Use a WordPress Plugin (Easiest Way)
Several plugins automatically minify JavaScript files:
✅ Best Plugins for JavaScript Minification:
- WP Rocket(Recommended)
- Go to Settings > WP Rocket > File Optimization.
- Enable Minify JavaScript.
- Optionally enable Combine JavaScript Files (only if not using HTTP/2).
- Test if enabling “Load JavaScript deferred” improves speed.
- Autoptimize
- Navigate to Settings > Autoptimize.
- Check Optimize JavaScript Code.
- Optionally enable Aggregate JS-files (but test as it can break some sites).
- Click Save Changes & Empty Cache.
- Fast Velocity Minify
- Good for combining/minifying assets, alternative to Autoptimize.
- Asset CleanUp: Page Speed Booster
- Lets you remove unnecessary scripts on pages where they are not needed.
- Minifies scripts along with reducing the number of requests.
2. Minify JavaScript Manually (If You Don’t Want a Plugin)
If you prefer manual minification, follow these steps:
✅ Step 1: Minify Your JavaScript File
- Use an online minifier like:
- Example:
// Original JS function greet(name) { console.log("Hello, " + name + "!"); } greet("Tokyo Blade");
Minified version:function greet(n){console.log("Hello, "+n+"!")}greet("Tokyo Blade");
✅ Step 2: Replace Unminified Files in WordPress
- Upload the minified JavaScript file (
.min.js
). - Modify your theme’s
functions.php
to load the minified version:function load_minified_js() { wp_enqueue_script('custom-js', get_template_directory_uri() . '/js/script.min.js', array(), null, true); } add_action('wp_enqueue_scripts', 'load_minified_js');
- Remove the old unminified file.
3. Use a Build Process for Minification (For Developers)
If you regularly update JavaScript, use a build tool to automate minification:
✅ Popular Tools for Minification
- Terser (CLI-based)
npx terser script.js -o script.min.js --compress --mangle
- UglifyJS
npx uglify-js script.js -o script.min.js --compress --mangle
- Gulp.js
- Install:
npm install --save-dev gulp gulp-uglify
- Gulp Task:
const gulp = require('gulp'); const uglify = require('gulp-uglify'); gulp.task('minify-js', function () { return gulp.src('js/*.js') .pipe(uglify()) .pipe(gulp.dest('js/min')); });
- Run:
gulp minify-js
- Install:
4. Enable GZIP or Brotli Compression (Further Reduce Size)
Even after minifying JavaScript, enable GZIP or Brotli compression to shrink files even more.
✅ Use .htaccess (For Apache)
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
</IfModule>
✅ Enable Brotli (For Nginx)
gzip on;
gzip_types application/javascript;
OR use WP Rocket, which enables GZIP compression automatically.

5. Defer JavaScript Execution for Faster Rendering
Once JavaScript is minified, defer non-critical scripts to prevent blocking page rendering.
✅ Use WP Rocket or Autoptimize
- WP Rocket → Enable Load JavaScript Deferred.
- Autoptimize → Enable Defer JS Loading.
✅ Manually Defer JS in functions.php
function defer_parsing_of_js($url) {
if (is_admin() || strpos($url, '.js') === false) return $url;
if (strpos($url, 'jquery.js')) return $url; // Exclude jQuery if required
return str_replace(' src', ' defer="defer" src', $url);
}
add_filter('script_loader_tag', 'defer_parsing_of_js', 10, 1);
6. Final Performance Check
After minifying JavaScript, test your site with:
- Google PageSpeed Insights (Test Here)
- GTmetrix (Test Here)
- Lighthouse (Chrome DevTools) →
F12
→ Lighthouse Tab → Run Performance Test.
✅ Key Metrics to Improve:
- Reduce Total Blocking Time (TBT)
- Improve Largest Contentful Paint (LCP)
- Reduce JavaScript Execution Time
🎯 Final Summary
✅ Easy Method → Use WP Rocket, Autoptimize, or Asset CleanUp
✅ Manual Method → Use online minifiers + edit functions.php
✅ Developer Method → Use Terser, UglifyJS, or Gulp.js
✅ Further Optimization → Enable GZIP/Brotli Compression + Defer JS