web-dev-qa-db-ja.com

HTTPヘッダー以降に変更された場合

私のWordPressサイトで304 If Modified Since HTTP headerを有効にしようとしています。 Googlingを何度も実行した後、著者がwordpressのwp-config.phpファイルの最後に次の行を追加すると言っているサイトを見つけました。これがコードの行です。

header("Last-Modified: " . the_modified_date());

今作者はこれがそれであると言った。私は304 If Modified Since HTTP headerを達成するために他に何もする必要はありません。しかしこれをした後私は場所を使用してHTTPヘッダーによってテストした

http://httpstatus.io/ /

これが私のヘッダーのスクリーンショットです。

enter image description here

赤い印のついた部分を確認してください。最後に変更されたヘッダー値はBLANKです。

その後、これはthe_modified_date()関数の問題であると思うので、get_the_modified_date()関数も試しました。しかし、まだ結果はありません。

最後に、これらの関数が機能しているかどうかをテストするための短いショートコード関数を作成し、それをショートコードの中にエコーしました。ショートコードを使用したとき、関数がうまく機能していることが明確にわかりますが、何らかの理由で304 If Modified Since HTTP headerに空白を送信します。

私のサイトは ここ

これを解決する方法について何か提案はありますか?

2
iSaumya

the_modified_date()はループの中で使わなければならないテンプレートタグです。

WordPressはHTTPヘッダを含めるか変更するためのアクションとフィルタフックを提供します。

しかし、それはこの目的のためには機能しません。たとえば、次のコードは機能しません。

add_action( 'send_headers', 'cyb_add_last_modified_header' );
function cyb_add_last_modified_header() {
    //Check if we are in a single post of any type (archive pages has not modified date)
    if( is_singular() ) {
        $post_id = get_queried_object_id();
        if( $post_id ) {
            header("Last-Modified: " . get_the_modified_time("D, d M Y H:i:s", $post_id) );
        }
    }
}

なんで?

メインのwpクエリは、現時点ではビルドされていません。またwp_headersフィルタでもビルドされていません。したがって、is_singular()falseを返し、get_queried_object_id()NULLを返し、現在の投稿の修正時刻を取得する方法はありません。

考えられる解決策は、 template_redirect actionフックを使用することです。これは、Ottoの この質問 (テスト済みで動作中)のとおりです。

add_action('template_redirect', 'cyb_add_last_modified_header');
function cyb_add_last_modified_header($headers) {

    //Check if we are in a single post of any type (archive pages has not modified date)
    if( is_singular() ) {
        $post_id = get_queried_object_id();
        if( $post_id ) {
            header("Last-Modified: " . get_the_modified_time("D, d M Y H:i:s", $post_id) );
        }
    }

}
3
cybmeta

Cybmetaの コードを試してみました しかし、日付が正しく設定されていませんでした。理由は完全にはわかりませんが、調査の結果、 Add Headers pluginが見つかりましたが、これは現在推奨されていませんが、便利なコードの優れたソースです。そのプラグインで、作者はLast-Modifiedヘッダの日付を別の方法で設定し、それを使ってコードを修正しました。これが私が最後になったものです:

add_action('template_redirect', 'cyb_add_last_modified_header');
function cyb_add_last_modified_header($headers) {
    //Check if we are in a single post of any type (archive pages has not modified date)
    if( is_singular() ) {
        $post_id = get_queried_object_id();
        if( $post_id ) {
            $post_mtime = get_the_modified_time("D, d M Y H:i:s", $post_id);
            $post_mtime_unix = strtotime( $post_mtime );
            $header_last_modified_value = str_replace( '+0000', 'GMT', gmdate('r', $post_mtime_unix) );
            header("Last-Modified: " . $header_last_modified_value );
        }
    }
}
2
Mojamba

私の解決策 - 最終更新日 - /および正しい 304未更新

Inside functions.php

add_action('template_redirect', 'last_mod_header');

function last_mod_header($headers) {
     if( is_singular() ) {
            $post_id = get_queried_object_id();
            $LastModified = gmdate("D, d M Y H:i:s \G\M\T", $post_id);
            $LastModified_unix = gmdate("D, d M Y H:i:s \G\M\T", $post_id);
            $IfModifiedSince = false;
            if( $post_id ) {
                if (isset($_ENV['HTTP_IF_MODIFIED_SINCE']))
                    $IfModifiedSince = strtotime(substr($_ENV['HTTP_IF_MODIFIED_SINCE'], 5));  
                if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
                    $IfModifiedSince = strtotime(substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 5));
                if ($IfModifiedSince && $IfModifiedSince >= $LastModified_unix) {
                    header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
                    exit;
                } 
     header("Last-Modified: " . get_the_modified_time("D, d M Y H:i:s", $post_id) );
                }
        }

    }
1
Travis Cat