Skip to content

Instantly share code, notes, and snippets.

@remyperona
Created September 15, 2017 13:55
Show Gist options
  • Save remyperona/b07c31d3a4b7bd4d08c4a1c052ef288a to your computer and use it in GitHub Desktop.
Save remyperona/b07c31d3a4b7bd4d08c4a1c052ef288a to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: No cache after logged-in user comment
* Description: Don't use cache after a user made a comment
* Author: WP Rocket team
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Basic security, prevents file from being loaded directly.
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );
function wp_rocket_set_loggedin_comment( $comment, $user ) {
if ( ! $user->exists() ) {
return;
}
$comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
setcookie( 'logged_in_comment_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
}
add_action( 'set_comment_cookies', 'wp_rocket_set_loggedin_comment', 10, 2 );
function wp_rocket_add_cookie( $cookies ) {
$cookies[] = 'logged_in_comment_';
return $cookies;
}
add_filter( 'rocket_cache_reject_cookies', 'wp_rocket_add_cookie' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment