X

How To Make the Protected Post or Page Cookie Expire With the Session

Snippets by IsItWP

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.

Comments   Leave a Reply

  1. Sergio Manetta April 11, 2013 at 9:06 am

    This code fucked up the rest of my site!! I removed it and now the passwords are not accepted!!

    1. when you enter a password what happens ?

      1. Sergio Manetta April 11, 2013 at 9:45 am

        Nothing, keeps asking again the password. Does this script also modify some other pages.php or files.php automatically ?

        1. this is the correct one

          setcookie(‘wp-postpass_’ . COOKIEHASH, $_COOKIE[‘wp-postpass_’ . COOKIEHASH], 0, COOKIEPATH);

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!