Created
April 23, 2019 06:45
-
-
Save enviado/406930a63b19761a20c627bc93d8c2fd to your computer and use it in GitHub Desktop.
HTTP Headers Last-Modified for WordPress
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 | |
| function HTTP_Headers_Last_Modified() { | |
| if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( is_admin() ) ) { | |
| return; | |
| } | |
| $last_modified = ''; | |
| if ( is_singular() ) { | |
| global $post; | |
| if ( !isset( $post -> post_modified_gmt ) ) { | |
| return; | |
| } | |
| $post_time = strtotime( $post -> post_modified_gmt ); | |
| $modified_time = $post_time; | |
| if ( ( int ) $post -> comment_count > 0 ) { | |
| $comments = get_comments( array( | |
| 'post_id' => $post -> ID, | |
| 'number' => '1', | |
| 'status' => 'approve', | |
| 'orderby' => 'comment_date_gmt', | |
| ) ); | |
| if ( !empty( $comments ) && isset( $comments[0] ) ) { | |
| $comment_time = strtotime( $comments[0] -> comment_date_gmt ); | |
| if ( $comment_time > $post_time ) { | |
| $modified_time = $comment_time; | |
| } | |
| } | |
| } | |
| $last_modified = str_replace( '+0000', 'GMT', gmdate( 'r', $modified_time ) ); | |
| } | |
| if ( is_archive() || is_home() ) { | |
| global $posts; | |
| if ( empty( $posts ) ) { | |
| return; | |
| } | |
| $post = $posts[0]; | |
| if ( !isset( $post -> post_modified_gmt ) ) { | |
| return; | |
| } | |
| $post_time = strtotime( $post -> post_modified_gmt ); | |
| $modified_time = $post_time; | |
| $last_modified = str_replace( '+0000', 'GMT', gmdate( 'r', $modified_time ) ); | |
| } | |
| if ( headers_sent() ) { | |
| return; | |
| } | |
| if ( !empty( $last_modified ) ) { | |
| header( 'Last-Modified: ' . $last_modified ); | |
| if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) && strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) >= $modified_time ) { | |
| $protocol = (isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'); | |
| header( $protocol . ' 304 Not Modified' ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment