Using WordPress ‘post_password_expires’ PHP filter

The post_password_expires WordPress PHP filter allows you to change the lifespan of the post password cookie.

Usage

add_filter('post_password_expires', 'your_function_name');
function your_function_name($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

This example sets the password cookie expiration to 5 days.

add_filter('post_password_expires', 'change_password_cookie_expiration');
function change_password_cookie_expiration($expires) {
    $expires = 5 * DAY_IN_SECONDS;
    return $expires;
}

This example sets the password cookie expiration to 1 hour.

add_filter('post_password_expires', 'set_one_hour_expiration');
function set_one_hour_expiration($expires) {
    $expires = HOUR_IN_SECONDS;
    return $expires;
}

This example turns the password cookie into a session cookie.

add_filter('post_password_expires', 'make_session_cookie');
function make_session_cookie($expires) {
    $expires = 0;
    return $expires;
}

This example sets the password cookie expiration to 30 days.

add_filter('post_password_expires', 'set_thirty_days_expiration');
function set_thirty_days_expiration($expires) {
    $expires = 30 * DAY_IN_SECONDS;
    return $expires;
}

This example sets different password cookie expirations based on the user role.

add_filter('post_password_expires', 'set_expiration_based_on_role');
function set_expiration_based_on_role($expires) {
    if (current_user_can('administrator')) {
        $expires = 30 * DAY_IN_SECONDS;
    } elseif (current_user_can('editor')) {
        $expires = 15 * DAY_IN_SECONDS;
    } else {
        $expires = 7 * DAY_IN_SECONDS;
    }
    return $expires;
}