Password Protected WordPress Pages

When you enter a password protected page, WordPress sets up a cookie that looks like wp-postpass_hash.

This cookie seems to hold forever. I would like to provide the user with a "log out" link. Is there a way to find and delete this cookie if I don't know the hash? Maybe there is a way to get a cookie by finding it based on "wp-postpass"?

I have seen other solutions for modifying core WordPress files to change the expiration date of a cookie, but this will not continue when there is an update. Maybe there is a function that I could write to change the expiration of this particular cookie?

Any tips? Thank!

+3
source share
4 answers
<?php
$pass_cookie='';
foreach($_COOKIE as $key=>$value){
  if(!strncmp($key,"wp-postpass_",12)){
    $pass_cookie = $key;
    break;
  }
}
if($pass_cookie){
  setcookie($pass_cookie,'',0);
}
?>

I would prefer to use something like the above - it does not depend on the position of the wp_postpass cookie

+1
source

This is good if you do not know the hash. In fact, the hash cookie is the MD5 blog URL. If you want to know your cookie hash of your site, try the following:

<?php

    $url="http://www.your-blog.com";
    $COOKIEHASH = md5($url);

    //Now, your cookie will be,
    echo 'wp-postpass_' . $COOKIEHASH; // Name of the cookie of your blog

    //Now, you can delete it whenever you want. :)

    //setting your cookie
    //setcookie('wp-postpass_' . $COOKIEHASH, $password, time() + 864000, COOKIEPATH);
?>
+3
source

Did you try to provide the user with a link to wp_logout_url()instead of programming it for yourself?

0
source

I ended up with this:

<?php
// turn all cookies into string
$cookie_string = implode(array_keys($_COOKIE));
// find position of desired cookie
$pos = strpos($cookie_string,'wp-postpass');
// extract string starting at $pos
$pass_cookie = substr($cookie_string,$pos);
// set cookie to expire on browser close
setcookie($pass_cookie,'',0);
?>

This is probably not the most elegant solution, especially because it requires the wp-postpass cookie to be the last in the array.

0
source

Source: https://habr.com/ru/post/1790073/


All Articles