Home  >  Article  >  CMS Tutorial  >  How does WordPress implement Gzip compression of js and css?

How does WordPress implement Gzip compression of js and css?

王林
王林Original
2019-11-04 10:59:592998browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn