The post_password_expires WordPress PHP filter allows you to change the life span of the post password cookie.
Usage
add_filter('post_password_expires', 'my_custom_post_password_expires');
function my_custom_post_password_expires($expires) {
// your custom code here
return $expires;
}
Parameters
$expires(int): The expiry time, as passed to setcookie().
More information
See WordPress Developer Resources: post_password_expires
Examples
Change the post password cookie expiry to 5 days
This code will set the life span of the post password cookie to 5 days.
add_filter('post_password_expires', 'change_post_password_expiry_to_5_days');
function change_post_password_expiry_to_5_days($expires) {
return 60 * 60 * 24 * 5; // 5 days in seconds
}
Set the post password cookie expiry to 1 hour
This code will set the life span of the post password cookie to 1 hour.
add_filter('post_password_expires', 'change_post_password_expiry_to_1_hour');
function change_post_password_expiry_to_1_hour($expires) {
return 60 * 60; // 1 hour in seconds
}
Make the post password cookie expire at the end of the session
This code will make the post password cookie a session cookie, expiring when the browser is closed.
add_filter('post_password_expires', 'set_post_password_cookie_to_session');
function set_post_password_cookie_to_session($expires) {
return 0;
}
Double the life span of the post password cookie
This code will double the default life span of the post password cookie (from 10 to 20 days).
add_filter('post_password_expires', 'double_post_password_cookie_life_span');
function double_post_password_cookie_life_span($expires) {
return $expires * 2;
}
Conditionally set the post password cookie expiry
This code will set the life span of the post password cookie to 1 day for a specific post, and 7 days for all other posts.
add_filter('post_password_expires', 'conditionally_set_post_password_cookie_expiry');
function conditionally_set_post_password_cookie_expiry($expires) {
if (get_the_ID() == 123) { // Specific post ID
return 60 * 60 * 24; // 1 day in seconds
} else {
return 60 * 60 * 24 * 7; // 7 days in seconds
}
}