Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use post_password_expires filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 1, 2023
5 minutes read

post_password_expires filter

Filters the life span of the post password cookie.

apply_filters( 'post_password_expires', int $expires )

Description

This is filter hook , which is used for filter the life span of the post password cookie.

Allows you to limit the cookie validity time either to a value passed as a parameter or to the session itself.
By default, the cookie expires 10 days from creation. To turn this into a session cookie, return 0.

Parameters

  • $expires : (int) The expiry time, as passed to setcookie().

Live Example

To run the hook, copy the example below.

$time = apply_filters( 'post_password_expires', $time ); 
                         
if ( !empty( $time ) ) { 
                         
   // everything has led up to this point... 
                         
} 
  

The following example is for adding a hook callback.

// define the post_password_expires callback 
function filter_post_password_expires( $time ) { 
    // make filter magic happen here... 
    return $time; 
}; 
         
// add the filter 
add_filter( 'post_password_expires', 'filter_post_password_expires', 10, 1 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_password_expires', 'filter_post_password_expires', 10, 1 ); 

 

function Modif_expir_cookie( $time ) { 
  return time() + 600 ;  // 10 mn
  // for 5 minutes :  
  // return time() + 300;  in this case 60 * 5 
  // return 0; set cookie to expire at the end of the session
}
add_filter('post_password_expires', 'Modif_expir_cookie' );
Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.