Merge files
Technique of merge files for css & js
Merge files
A WordPress site usually has a lot of js & CSS files, and the loading of such resources will affect the speed of the page. The solution is to merge all local JS files into a single file ( the same applies to CSS). Note: only merge files via the hook wp_enqueue_script
wp_enqueue_style
In the principle of optimization, we should not edit directly to js, css files. So all you need to do is editing on the newly created file. However, the merged js / css file has quite long content, and makes it difficult for you to read or modify it. Do not worry, we have a tons of api to help you walk on every file before they are merged into a new file. See the example below:
Merge CSS files:
There are a few customers who complain to us that after doing merge files for CSS the page looks different to the original version. Why? remember that: in order to merged CSS files to work properly 100% , all single CSS files should have a valid syntax in CSS code.
So you don't forget to check on every CSS file that used in merges, and fix any error CSS syntax before the merge works. After removing the error code go to Dashboard > Settings > Page Speed Optimizer > and click to the [purge all] button to re-create all merge files.
Exclude file from merging
For example, we have JS code, you want to exclude the frontend-gtag.min.js script from merged or delay. See the original code:
We can exclude this file from merge and delay by using the hook below:
Customization
Many people got error JS in the Chrome dev tool after merges files. Because of wrong js files in order. With Penci Speed Optimizer you can modify dependencies of scripts to make ordering your own & other file attributes to control everything before they got right to merge.
Exclude Lazy JS
For external JS files (which not store in current site hosting) can't be merged even it's appended by using the function wp_enqueue_script
. Insert these files to the page in dynamic ways, by the way so you can't see <script tag when view-source because static <script tag was removed.
Any enqueue internal js files (which<script
tags has src
attribute), it should be merged but some js file need to keep <script tag. For example: embed code of getresponse service to print out the form on the document in same location on the page that script tag reside. In this case, you will need to remove lazy feature for this script.
Defer/lazyload JS
The way of keeping <script tag & all you need to do is rename src
attribute to data-src
Using the hook hpp_prepare_buffer_html
to catch full html of the page after php output result to the browser. So now i will do lazyload with above example.
Last updated