- サイトデザイン工事中です。ご意見をお寄せください。
- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
WordPress の最適化/出力の圧縮
提供:WordPress Codex 日本語版
WordPress 自体にはブログの HTML 出力を圧縮する仕組みがありません。これは通常、Web サーバー(例: Apache の圧縮有効化)や、PHP スクリプト言語の設定( zlib)などに基づいて提供される機能です。
.htaccess による有効化
Apache サーバーの出力圧縮を細かく設定したい場合、以下の .htaccess 向けコードスニペットが役に立つかもしれません。
<IfModule mod_deflate.c> # Insert filters AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/x-httpd-php AddOutputFilterByType DEFLATE application/x-httpd-fastphp AddOutputFilterByType DEFLATE image/svg+xml # Drop problematic browsers BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary </IfModule>
PHP コードによる有効化
サーバーの設定ファイルを編集できない場合、PHP コード内で圧縮を有効化する方法もあります。WordPress をインストールしたルートディレクトリの index.php ファイルの最初に一行、ob_start('ob_gzhandler'); というコードを書くだけです。
<?php
ob_start('ob_gzhandler');
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
[...]
注: より良い結果を得るため、テーマの index.php ではなく、メインの index.php ファイルを編集すべきです。したがって、このファイルは次に WordPress をアップグレードした際に上書きされてしまうのを忘れないようにしてください。その際、再度編集が必要になります。
上記のコードが動作するには、PHP でZlib 圧縮関数が使えるようになっている必要があります。
参照
- コンポーネントを圧縮しよう! - dogmap.jp
- Compressing WordPress Output (ady、2008年7月3日)
- Content Compression Using PHP (Paul Katsandem、2007年3月3日)
- Gzip 圧縮に関連するチケット #10365
- WordPress パフォーマンスのテスト / en
最新英語版: WordPress Codex » Output_Compression (最新版との差分)