I’ve searched high and low for an extension which displays a link that deletes this cookie, but nothing turned up. So I simply modified “wp_pass.php” a little and wrote a small function.

First, create a file named “wp_passexpire.php” with the following:

<?php
require( dirname(__FILE__) . ‘/wp-config.php’);

// Expire
setcookie(’wp-postpass_’ . COOKIEHASH, ”, time() - 864000, COOKIEPATH);

wp_cache_delete($blog_id);

wp_safe_redirect(wp_get_referer());
?>

Second, put the following in “function.php” of your theme:

function pass_expire($before=”", $text=”Delete”, $after=”") {
global $post;
if (!empty($post->post_password)) { // if there’s a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] == $post->post_password) { // and it matches the cookie
echo $before . ‘<a href=”/wp-passexpire.php”>’ . $text . ‘</a>’ . $after;
}
}
}

And lastly, call it with:

<?php pass_expire(’ | ‘, ‘Delete Cookie’, ‘ | ‘); ?>

Test it out, password is “test”. :)

Share This Post:
  • Digg
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Spurl
  • Facebook
  • Google
  • LinkedIn

Related Posts:
  1. Private entries revisited...
  2. Upgrade Your WordPress...
  3. Private entry hack...
  4. New b2comments hack...
  5. Word Count Patch...

8 Comments

  1. George said:

    Hello Michael,

    I have an error in the functions.php with your script:
    Parse error: parse error, unexpected T_STRING, expecting ‘,’ or ‘;’ in D:\EasyPHP20\www\wordpress\wp-content\themes\default\functions.php on line xx

    Plz help…

  2. Michael said:

    George, I don’t see anything wrong with my code… contact me.

  3. Nathan said:

    I’m getting the say error when putting that block of code into functions.php

    did you figure out what was wrong?

    thanks for this work around

  4. Michael said:

    WP was converting single quotes into ticks. Be sure to check for this.

  5. Anja said:

    Hi, can this fix my problem - That public computers will have access to my password-protected posts because of the cookie?

    I’d just like the cookie to be deleted when the browser is closed I guess.

    Cheers =)

  6. Michael said:

    Anja » That can only be done on the client side unless WP use session cookies.

  7. Anja said:

    I found my solution a few minutes ago. In wp-pass.php, i just set the seconds to “600″, 10 minutes, which works fine for me. I guess it was so simple that no one had bothered to mention it anywhere! But that’s all I needed =)

  8. Michael said:

    Anja » The downside of doing that is your friends will have to memorize the password. But it works. :)

Leave a Reply