PHP delete cookie that starts with wp_postpass_

I need to write some custom code for a WordPress function, and I need to be able to delete any cookies that start with wp-postpass_. I know this can be done with jQuery but I’m unsure of how to approach it in PHP.

I have tried Googling and searching on here but I haven’t been able to find anything that matches what I’m trying to do.

Read More

Thank you in advance,
Andy

EDIT:
Sorry, I should have mentioned that WordPress appends a random string onto the end of wp-postpass_ hence why I need to find any cookies that start with wp-postpass_. Apologies, early morning.

Related posts

2 comments

  1. So iterate through all cookies and check if there contain wp_postpass_ and then remove the cookie.

    foreach($_COOKIE as $cookieKey => $cookieValue) {
        if(strpos($cookieKey,'wp-postpass_') === 0) {
            // remove the cookie
            setcookie($cookieKey, null, -1);
            unset($_COOKIE[$cookieKey]);
        }
    }
    
  2. If you have access to the $_COOKIE superglobal just do

    $past = time() - 86400;
    foreach($_COOKIE as $name => $value) {
        if(strpos($name, 'wp-postpass_') === 0) {
            setcookie($name, '', $past);
            unset($_COOKIE[$name]);
        }
    }
    

Comments are closed.