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

How does WordPress implement Gzip compression of js and css?

王林
Release: 2019-11-04 10:59:59
Original
3104 people have browsed it

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”); ?>
Copy after login

Add the following code at the end:

<?php if(extension_loaded(‘zlib’)) {ob_end_flush();} ?>
Copy after login

2. Modify the css connection in header.php as follows

Original:

<link rel=”stylesheet” type=”text/css” media=”screen” href=”/style.css”/>
Copy after login

Modified:

<link rel=”stylesheet” type=”text/css” media=”screen”href=”/style.css.php”/>
Copy after login

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”); ?>
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template