The comment_cookie_lifetime WordPress PHP filter allows you to modify the lifetime of the comment cookie in seconds.
Usage
add_filter( 'comment_cookie_lifetime', 'my_custom_comment_cookie_lifetime' ); function my_custom_comment_cookie_lifetime( $seconds ) { // your custom code here return $seconds; }
Parameters
$seconds
(int) – The current comment cookie lifetime in seconds. Default is 30000000.
More information
See WordPress Developer Resources: comment_cookie_lifetime
Examples
Extend comment cookie lifetime to 60 days
To extend the comment cookie lifetime to 60 days:
add_filter( 'comment_cookie_lifetime', 'extend_comment_cookie_lifetime' ); function extend_comment_cookie_lifetime( $seconds ) { return 60 * 24 * 60 * 60; // 60 days in seconds }
Shorten comment cookie lifetime to 1 day
To shorten the comment cookie lifetime to 1 day:
add_filter( 'comment_cookie_lifetime', 'shorten_comment_cookie_lifetime' ); function shorten_comment_cookie_lifetime( $seconds ) { return 24 * 60 * 60; // 1 day in seconds }
Disable comment cookies
To completely disable comment cookies, set the lifetime to 0:
add_filter( 'comment_cookie_lifetime', 'disable_comment_cookies' ); function disable_comment_cookies( $seconds ) { return 0; // disable comment cookies }
Double the comment cookie lifetime
To double the current comment cookie lifetime:
add_filter( 'comment_cookie_lifetime', 'double_comment_cookie_lifetime' ); function double_comment_cookie_lifetime( $seconds ) { return $seconds * 2; // double the lifetime }
Set comment cookie lifetime based on user role
To set different comment cookie lifetimes based on user role:
add_filter( 'comment_cookie_lifetime', 'user_role_based_comment_cookie_lifetime' ); function user_role_based_comment_cookie_lifetime( $seconds ) { if ( current_user_can( 'administrator' ) ) { return 365 * 24 * 60 * 60; // 1 year for administrators } elseif ( current_user_can( 'editor' ) ) { return 180 * 24 * 60 * 60; // 6 months for editors } else { return 30 * 24 * 60 * 60; // 1 month for other users } }