Using WordPress ‘recovery_mode_cookie_length’ PHP filter

The ‘recovery_mode_cookie_length’ is a WordPress PHP filter that allows you to modify the duration of a Recovery Mode cookie’s validity.

Usage

To use this filter, add the following code to your theme’s functions.php file or a custom plugin:

add_filter( 'recovery_mode_cookie_length', 'custom_recovery_mode_cookie_length' );

function custom_recovery_mode_cookie_length( $length ) {
    // Your custom code here
    return $length;
}

Parameters

  • $length (int) – The length of time, in seconds, that the Recovery Mode cookie is valid.

Examples

add_filter( 'recovery_mode_cookie_length', 'extend_recovery_mode_cookie_length' );

function extend_recovery_mode_cookie_length( $length ) {
    return 86400; // 24 hours * 60 minutes * 60 seconds
}

In this example, the extend_recovery_mode_cookie_length function sets the Recovery Mode cookie length to 1 day (86400 seconds).

add_filter( 'recovery_mode_cookie_length', 'shorten_recovery_mode_cookie_length' );

function shorten_recovery_mode_cookie_length( $length ) {
    return 3600; // 60 minutes * 60 seconds
}

In this scenario, the shorten_recovery_mode_cookie_length function reduces the Recovery Mode cookie length to just 1 hour (3600 seconds).

add_filter( 'recovery_mode_cookie_length', 'double_recovery_mode_cookie_length' );

function double_recovery_mode_cookie_length( $length ) {
    return $length * 2;
}

Here, the double_recovery_mode_cookie_length function doubles the current Recovery Mode cookie length.

add_filter( 'recovery_mode_cookie_length', 'user_role_based_recovery_mode_cookie_length' );

function user_role_based_recovery_mode_cookie_length( $length ) {
    if ( current_user_can( 'administrator' ) ) {
        return 86400;
    } else {
        return 3600;
    }
}

In this example, the user_role_based_recovery_mode_cookie_length function sets the Recovery Mode cookie length to 1 day for administrators and 1 hour for all other users.

add_filter( 'recovery_mode_cookie_length', 'disable_recovery_mode_cookie' );

function disable_recovery_mode_cookie( $length ) {
    return 0;
}

In this scenario, the disable_recovery_mode_cookie function disables the Recovery Mode cookie by setting its length to 0 seconds.