Each time a visitor leaves a comment on your WordPress blog, it automatically stores a cookies – comment_author, comment_author_email and comment_author_url. So each time that particular visitor returns and want to leave another comment, the person don’t have to fill out those necessary ‘Personal Information’ (Name, E-mail and URL) again on the comment form.
According to WordPress Codex, WordPress checks for the existence of their cookie, and then tries to compare their hashed data with the values stored in the WordPress database
.
We can actually use it to do some cool stuff. Here are some of the examples:
- Code snippet 1:
<h3> <?php if ( ($comment_author != "") && !is_user_logged_in() ) { ?> Want to share something <?php echo $comment_author; ?>? <?php } else { ?> Share Your Comments <?php } ?> </h3>Based on the condition, IF the
$comment_authorvariable is NOT empty and NOT logged-in user; display Want to share something COMMENT_AUTHOR? or ELSE display Share Your Comments. - Code snippet 2:
If you want to use it outside comments for example on the front page:
if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) { $comment_author = $_COOKIE['comment_author_'.COOKIEHASH]; echo 'Welcome back, '.$comment_author.'!'; }It will display as Welcome back, COMMENT_AUTHOR!
- Code snippet 3:
Do not display advertisement to returning visitor and logged-in user.
global $cookiehash; if ( !isset($_COOKIE['comment_author_'.COOKIEHASH]) && !is_user_logged_in() ) { echo 'Advertisement goes here..'; }