{"id":2645,"date":"2021-05-28T14:57:32","date_gmt":"2021-05-28T14:57:32","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=2645"},"modified":"2023-02-28T04:45:51","modified_gmt":"2023-02-28T04:45:51","slug":"how-to-minify-css-javascript-files-in-wordpress-3-ways","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/how-to-minify-css-javascript-files-in-wordpress-3-ways\/","title":{"rendered":"How to Minify CSS \/ JavaScript Files in WordPress (3 Ways)"},"content":{"rendered":"<p>Do you want to minify files on your WordPress site?<\/p>\n<p>Minifying your WordPress CSS and JavaScript files can make them load faster and speed up your WordPress site.<\/p>\n<p>In this guide, we will show you how to easily minify CSS\/JavaScript files in WordPress to improve performance and speed.<\/p>\n<h4>What is Minification and When Do You Need it?<\/h4>\n<p>The term \u2018minify\u2019 is used to describe a method that makes your website file sizes smaller. It does this by removing white spaces, lines, and unnecessary characters from the source code.<\/p>\n<p>Here is an example of normal CSS code:<\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<div>\n<div id=\"highlighter_417713\" class=\"syntaxhighlighter css\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"css plain\">body {<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"css keyword\">margin<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">20px<\/code><code class=\"css plain\">;<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"css keyword\">padding<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">20px<\/code><code class=\"css plain\">;<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"css keyword\">color<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">#333333<\/code><code class=\"css plain\">;<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"css keyword\">background<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">#f7f7f7<\/code><code class=\"css plain\">;<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"css plain\">}<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"css plain\">h<\/code><code class=\"css value\">1<\/code> <code class=\"css plain\">{<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"css keyword\">font-size<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">32px<\/code><code class=\"css plain\">;<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"css plain\">color<\/code><code class=\"css value\">#222222<\/code><code class=\"css plain\">;<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"css keyword\">margin-bottom<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">10px<\/code><code class=\"css plain\">;<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"css plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>After minifying the code it will look like this:<\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<div>\n<div id=\"highlighter_802932\" class=\"syntaxhighlighter css\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"css plain\">body{<\/code><code class=\"css keyword\">margin<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">20px<\/code><code class=\"css plain\">;<\/code><code class=\"css keyword\">padding<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">20px<\/code><code class=\"css plain\">;<\/code><code class=\"css keyword\">color<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">#333<\/code><code class=\"css plain\">;<\/code><code class=\"css keyword\">background<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">#f7f7f7<\/code><code class=\"css plain\">}<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"css plain\">h<\/code><code class=\"css value\">1<\/code><code class=\"css plain\">{<\/code><code class=\"css keyword\">font-size<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">32px<\/code><code class=\"css plain\">;<\/code><code class=\"css keyword\">margin-bottom<\/code><code class=\"css plain\">:<\/code><code class=\"css value\">10px<\/code><code class=\"css plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>Usually, it is recommended to only minify files that are sent to user\u2019s browsers. This includes HTML,\u00a0CSS, and\u00a0JavaScript\u00a0files.<\/p>\n<p>You can minify PHP files too, but minifying it will not improve page load speed for your users. That\u2019s because\u00a0PHP\u00a0is a server-side programming language, meaning it runs on servers before anything is sent to the visitor\u2019s web browser.<\/p>\n<p>The advantage of minifying files is\u00a0improved WordPress speed and performance, since compact files are faster to load.<\/p>\n<p>However, some experts believe that the performance improvement is very small for most websites and not worth the trouble. Minification only removes a few kilobytes of data on most WordPress sites. You can reduce more page load time by\u00a0optimizing images for the web.<\/p>\n<p>If you are trying to achieve 100\/100 score on Google Pagespeed or\u00a0GTMetrix tool, then minifying CSS and JavaScript will significantly improve your score.<\/p>\n<p>Having said that, let\u2019s take a look at how to easily minify CSS\/JavaScript on your WordPress site.<\/p>\n<p>We\u2019ll go over 3 different options you can choose from:<\/p>\n<ul>\n<li>Method 1. Minify CSS\/JavaScript in WordPress Using WP Rocket (Recommended)<\/li>\n<li>Method 2. Minify CSS\/JavaScript in WordPress Using SiteGround<\/li>\n<li>Method 3. Minify CSS\/JavaScript using Autoptimize<\/li>\n<\/ul>\n<p>Ready? Let\u2019s get started with our top recommended method.<\/p>\n<h4 id=\"minify-using-wp-rocket\">Method 1. Minify CSS\/JavaScript in WordPress Using WP Rocket<\/h4>\n<p>This method is easier and is recommended for all users. It works regardless of which\u00a0WordPress hosting\u00a0you are using.<\/p>\n<p>First, you need to install and activate the\u00a0WP Rocket\u00a0plugin. For more details, see our step by step guide on\u00a0how to install a WordPress plugin.<\/p>\n<p>WP Rocket\u00a0is the best WordPress caching plugin on the market. It allows you to easily add caching to WordPress and significantly improve website speed and page load times.<\/p>\n<p>For more details, see our tutorial on\u00a0how to install and set up WP Rocket\u00a0in WordPress.<\/p>\n<p>Upon activation, you need to visit the\u00a0<strong>Settings \u00bb WP Rocket<\/strong>\u00a0page and switch to the \u2018File Optimization\u2019 tab.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116341 lazyloaded\" title=\"Minify CSS files in WP Rocket\" src=\"https:\/\/cdn3.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-css-minify.png\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn3.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-css-minify.png 680w, https:\/\/cdn2.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-css-minify-300x139.png 300w\" alt=\"Minify CSS files in WP Rocket\" width=\"680\" height=\"316\" data-ll-status=\"loaded\" \/><\/figure>\n<p>From here, you need to check the Minify CSS files option.<\/p>\n<p>WP Rocket will then show you a warning that this could break things on your site. Go ahead and click on the \u2018Activate Minify CSS\u2019 button. You can always deactivate this option if it causes any issues with your website.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116342 lazyloaded\" title=\"Activate CSS minify\" src=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-activate-minify.png\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-activate-minify.png 680w, https:\/\/cdn4.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-activate-minify-300x155.png 300w\" alt=\"Activate CSS minify\" width=\"680\" height=\"352\" data-ll-status=\"loaded\" \/><\/figure>\n<p>Next, you need to scroll down to the JavaScript Files section below.<\/p>\n<p>Here, simply check the box next to the \u2018Minify JavaScript files\u2019 option.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116343 lazyloaded\" title=\"WP Rocket minify JavaScript files\" src=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-minify-js.png\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-minify-js.png 680w, https:\/\/cdn3.wpbeginner.com\/wp-content\/uploads\/2017\/03\/wprocket-minify-js-300x143.png 300w\" alt=\"WP Rocket minify JavaScript files\" width=\"680\" height=\"325\" data-ll-status=\"loaded\" \/><\/figure>\n<p>Again, you\u2019ll see a warning that this could break things on your site. Go ahead and click on the \u2018Activate Minify JavaScript\u2019 button.<\/p>\n<p>After that, don\u2019t forget to click on the Save changes button to store your settings.<\/p>\n<p>WP Rocket will now start minifying your CSS and JavaScript files. You can\u00a0clear your cache\u00a0in the plugin settings to make sure that it starts using the minified CSS and JavaScript for the next website visitor.<\/p>\n<h4 id=\"minify-using-siteground-plugin\">Method 2. Minify CSS\/JavaScript in WordPress Using SiteGround<\/h4>\n<p>If you are using\u00a0SiteGround\u00a0as your\u00a0WordPress hosting\u00a0provider, then you can minify CSS files using\u00a0SiteGround\u00a0Optimizer.<\/p>\n<p>SiteGround Optimizer is a performance optimization plugin that works on their own platform. It works well with their\u00a0Ultrafast PHP\u00a0to improve your site\u2019s loading times.<\/p>\n<p>Simply install and activate the\u00a0<a title=\"SiteGround Optimizer\" href=\"https:\/\/wordpress.org\/plugins\/sg-cachepress\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">SiteGround Optimizer<\/a>\u00a0plugin on your WordPress site. For more details, see our step by step guide on\u00a0how to install a WordPress plugin.<\/p>\n<p>After that, you need to click on the SG Optimizer menu in your WordPress admin sidebar.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116347 lazyloaded\" title=\"SG Optimizer\" src=\"https:\/\/cdn3.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sgoptimizer.jpg\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn3.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sgoptimizer.jpg 680w, https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sgoptimizer-300x146.jpg 300w\" alt=\"SG Optimizer\" width=\"680\" height=\"330\" data-ll-status=\"loaded\" \/><\/figure>\n<p>This will take you to SG Optimizer settings.<\/p>\n<p>From here you need to click on the \u2018Go To Frontend\u2019 button under \u2018Other Optimizations\u2019.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116348 lazyloaded\" title=\"SiteGround frontend optimization\" src=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sg-frontend-optimizations.png\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sg-frontend-optimizations.png 680w, https:\/\/cdn3.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sg-frontend-optimizations-300x129.png 300w\" alt=\"SiteGround frontend optimization\" width=\"680\" height=\"293\" data-ll-status=\"loaded\" \/><\/figure>\n<p>On the next screen, you need to switch on the toggle next to the \u2018Minify CSS files\u2019 option.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116349 lazyloaded\" title=\"Minify CSS in SiteGround\" src=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sg-minify-css.png\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sg-minify-css.png 680w, https:\/\/cdn4.wpbeginner.com\/wp-content\/uploads\/2017\/03\/sg-minify-css-300x89.png 300w\" alt=\"Minify CSS in SiteGround\" width=\"680\" height=\"202\" data-ll-status=\"loaded\" \/><\/figure>\n<p>Next, you need to switch to the JavaScript tab and turn on the toggle next to \u2018Minify JavaScript Files\u2019 option.<\/p>\n<p>That\u2019s all! You can now empty your WordPress cache and visit your website to load minified versions of CSS and JS files.<\/p>\n<h4 id=\"minify-using-autoptimize-plugin\">Method 3. Minify CSS\/JavaScript using Autoptimize<\/h4>\n<p>This method is recommended for users who are not on SiteGround and not using WP Rocket.<\/p>\n<p>First, you need to install and activate the\u00a0<a href=\"https:\/\/wordpress.org\/plugins\/autoptimize\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Autoptimize<\/a>\u00a0plugin. For more details, see our step by step guide on\u00a0how to install a WordPress plugin.<\/p>\n<p>Upon activation, you need to visit the\u00a0<strong>Settings \u00bb Autoptimize<\/strong>\u00a0page to configure the plugin settings.<\/p>\n<p>From here, first you need to check the option \u2018Optimize JavaScript Code\u2019 under JavaScript Options.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116345 lazyloaded\" title=\"Autoptimize JavaScript options\" src=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/autoptimize-js-options.png\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn.wpbeginner.com\/wp-content\/uploads\/2017\/03\/autoptimize-js-options.png 680w, https:\/\/cdn2.wpbeginner.com\/wp-content\/uploads\/2017\/03\/autoptimize-js-options-300x143.png 300w\" alt=\"Autoptimize JavaScript options\" width=\"680\" height=\"325\" data-ll-status=\"loaded\" \/><\/figure>\n<p>After that, you need to scroll down to the CSS Options and check the box next to the \u2018Optimize CSS code\u2019 option.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116346 lazyloaded\" title=\"Autoptimize CSS code\" src=\"https:\/\/cdn2.wpbeginner.com\/wp-content\/uploads\/2017\/03\/autoptimize-css.png\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" srcset=\"https:\/\/cdn2.wpbeginner.com\/wp-content\/uploads\/2017\/03\/autoptimize-css.png 680w, https:\/\/cdn4.wpbeginner.com\/wp-content\/uploads\/2017\/03\/autoptimize-css-300x119.png 300w\" alt=\"Autoptimize CSS code\" width=\"680\" height=\"269\" data-ll-status=\"loaded\" \/><\/figure>\n<p>Don\u2019t forget to click on Save Changes button to store your settings.<\/p>\n<p>Then you can click the Empty Cache button to start using your minified files. The plugin can also be used to\u00a0fix render blocking JavaScript and CSS\u00a0in WordPress.<\/p>\n<p>We hope this article helped you minify CSS and JavaScript on your WordPress site. You may also want to see our tutorial on\u00a0optimizing core web vitals in WordPress\u00a0and follow our ultimate\u00a0WordPress performance guide.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to minify files on your WordPress site? Minifying your <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/how-to-minify-css-javascript-files-in-wordpress-3-ways\/\" title=\"How to Minify CSS \/ JavaScript Files in WordPress (3 Ways)\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3820,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,20,17],"tags":[119,132,133,114],"newstopic":[],"class_list":{"0":"post-2645","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-html-css","8":"category-javascript","9":"category-wordpress","10":"tag-design-tips","11":"tag-minify-css","12":"tag-minify-javascript","13":"tag-web-design-trends"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/2645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=2645"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/2645\/revisions"}],"predecessor-version":[{"id":2647,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/2645\/revisions\/2647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3820"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=2645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=2645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=2645"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=2645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}