Last active
April 27, 2021 01:52
-
-
Save waviaei/bad141aa55864361ef9cb5e5d12a1805 to your computer and use it in GitHub Desktop.
HTTP ヘッダーに Cache-Control ディレクティブを出力する例。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * HTTP ヘッダーに Cache-Control ディレクティブを出力する例。 | |
| */ | |
| add_action( 'template_redirect', function() { | |
| // ログイン済みなら何もしない | |
| if ( is_user_logged_in() ) { | |
| return; | |
| } | |
| // max-age の値を秒で指定. | |
| $ttl = 0; | |
| if ( is_front_page() ) { | |
| // フロントページ | |
| $ttl = 120; | |
| } elseif( is_singular() && ! is_page() ) { | |
| $ttl = 120; | |
| } elseif ( is_tax() ) { | |
| $ttl = 300; | |
| } | |
| // max-age の値にn 0 秒以上が設定されている場合はHTTPヘッダーに出力. | |
| if ( 0 < $ttl ) { | |
| header( sprintf( 'Cache-Control: max-age=%d', $ttl ) ); | |
| } | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment