Are you looking for a way to make the protected post or page cookie expire with the session? This snippet forces the protected post/page cookie to expire with the session by setting the cookie’s expiration time to 0.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
add_action( 'wp', 'post_pw_sess_expire' ); function post_pw_sess_expire() { if ( isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) ) // Setting a time of 0 in setcookie() forces the cookie to expire with the session setcookie('wp-postpass_' . COOKIEHASH, '', 0, COOKIEPATH); }
This snippet was created by Drew Jaynes.
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 6 Best WordPress Security Plugins Compared and how to use Google Optimize in WordPress.
This code fucked up the rest of my site!! I removed it and now the passwords are not accepted!!
when you enter a password what happens ?
Nothing, keeps asking again the password. Does this script also modify some other pages.php or files.php automatically ?
this is the correct one
setcookie(‘wp-postpass_’ . COOKIEHASH, $_COOKIE[‘wp-postpass_’ . COOKIEHASH], 0, COOKIEPATH);