29 Jun, 2004
Forget User Info
Posted by: Jennifer In: WordPress my-hacks additions|WordPress scripts
When someone leaves a comment on a WordPress blog, a cookie is saved to their computer with the name, e-mail, and URI that they entered. This is quite handy for most people, but it may bother those who use public or shared computers. WordPress does not by default have a way for commenters to specify that they don't want their info saved to a cookie, so here's a way to do that.
Warning: This is not just a plugin. You are going to add a few lines to a core WordPress file. Just so you know.
2 files need to be modified: wp-comments.php (to put in the checkbox for "don't remember my info" and the "delete my cookie" link), and wp-comments-post.php (to ensure that the cookie isn't saved if the user requests that it not be), and you need to install a plugin to handle manual "delete my cookie" requests. (Just a side note, and this applies to anytime you need to modify an existing page, you should make backups of the wp-comments.php, and wp-comments-post.php page before you make your changes)
Step 1
Open up wp-comments-post.php and find these lines (89, 90 and 91):
setcookie('comment_author_' . $cookiehash, $author, time() + 30000000, COOKIEPATH);
setcookie('comment_author_email_' . $cookiehash, $email, time() + 30000000, COOKIEPATH);
setcookie('comment_author_url_' . $cookiehash, $url, time() + 30000000, COOKIEPATH);
Change those lines to the following:
if($_POST['forget_cookie'] == 'forget'){
setcookie('comment_author_' . $cookiehash, ", time()-60000, COOKIEPATH);
setcookie('comment_author_email_' . $cookiehash, ", time()-60000, COOKIEPATH);
setcookie('comment_author_url_' . $cookiehash, ", time()-60000, COOKIEPATH);
} else {
setcookie('comment_author_' . $cookiehash, $author, time() + 30000000, COOKIEPATH);
setcookie('comment_author_email_' . $cookiehash, $email, time() + 30000000, COOKIEPATH);
setcookie('comment_author_url_' . $cookiehash, $url, time() + 30000000, COOKIEPATH);
}
Save wp-comments-post.php and upload the new version.
Step 2
Add the following code to your my-hacks.php.
/*
Code Reference Name: Forget User Info
URI: http://www.scriptygoddess.com/archives/2004/06/29/forget-user-info/
Description: Deletes the comment cookie when ?forget_user_info=1 is passed to the URL
Version: 1.0
Author: Mark Jaquith
Author URI: http://www.txfx.net/
*/
if($_GET['forget_user_info']) { // User has manually requested that their cookie be deleted.
setcookie('comment_author_' . $cookiehash, ", time()-60000, COOKIEPATH); setcookie('comment_author_email_' . $cookiehash, ", time()-60000, COOKIEPATH); setcookie('comment_author_url_' . $cookiehash, ", time()-60000, COOKIEPATH);
unset($comment_author, $comment_author_email, $comment_author_url);
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
$location = $_SERVER["HTTP_REFERER"];
if ($is_IIS) {
header("Refresh: 0;url=$location");
} else {
header("Location: $location");
}
}
Step 3
Open up wp-comments.php.
Add this line, somewhere in the form section:
(You should also probably give the input a tabindex, and reorder any other input tabindexes that follow, so that your users don't become human pinballs when they press TAB to go to the next form element.)
<p>
<input type="checkbox" name="forget_cookie" id="forget_cookie" value="forget" />
<label for="forget_cookie">Do not save my info</label>
</p>
Add this line where you think it would be appropriate:
<?php if (strlen($comment_author) != 0 || strlen($comment_author_email) != 0 || strlen($comment_author_url) != 0) { echo '<p>Your info has been saved, but you may <a href="' . $_SERVER['REQUEST_URI'] . '?forget_user_info=1">delete your cookie</a> if you wish.</p>'; } ?>
Save your changes to wp-comments.php, upload the new version, and you're done!