Skip to content

Instantly share code, notes, and snippets.

@waviaei
Last active April 27, 2021 01:52
Show Gist options
  • Select an option

  • Save waviaei/bad141aa55864361ef9cb5e5d12a1805 to your computer and use it in GitHub Desktop.

Select an option

Save waviaei/bad141aa55864361ef9cb5e5d12a1805 to your computer and use it in GitHub Desktop.
HTTP ヘッダーに Cache-Control ディレクティブを出力する例。
<?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