Home > Article > CMS Tutorial > How does WordPress implement Gzip compression of js and css?
1. Copy style.css in the template directory, name it style.css.php, and then add this sentence at the top of style.css.php:
<?php if(extension_loaded(‘zlib’)) {ob_start(‘ob_gzhandler’);}header(“Content-type: text/css”); ?>
Add the following code at the end:
<?php if(extension_loaded(‘zlib’)) {ob_end_flush();} ?>
2. Modify the css connection in header.php as follows
Original:
<link rel=”stylesheet” type=”text/css” media=”screen” href=”/style.css”/>
Modified:
<link rel=”stylesheet” type=”text/css” media=”screen”href=”/style.css.php”/>
In this way, your CSS is compressed by Gzip, and your JS can be compressed in the same way. Just add the following code at the top of JS:
<?php if ( extension_loaded(‘zlib’) ) {ob_start(‘ob_gzhandler’);}header(“Content-Type: text/javascript”); ?>
Recommended tutorial: wordpress tutorial
The above is the detailed content of How does WordPress implement Gzip compression of js and css?. For more information, please follow other related articles on the PHP Chinese website!