Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

From within a WordPress plugin, is there any way to get the name and email of the current user, when s/he is not logged in, but has recently submitted a comment, and specified name and email in the comment reply form?

Or is the name & email only available during the HTTP request in which [the comment posted by the user] is being processed?

(When the the user is not logged in, the WP_User returned by wp_get_current_user() contains no info on the user's email or name, I've noticed when debugging.)

(Background: I'm writing a comment rating plugin, and I'd like to highlight the user's ratings, even if s/he isn't a registered user. As mentioned in the comments below, perhaps some cookie will be needed in any case. I'm still interested in knowing the answer to this question however, perhaps I could reuse some existing functionality or something)

share|improve this question
    
I think you could probably set some kind of cookie, however users might not like still being half logged in to your site after they log out. –  fdsa Jul 15 '12 at 2:18
    
How are they authenticated to comment? –  fdsa Jul 15 '12 at 2:36
    
@fdsa re: authentication: they probably specify name + email (or log in, if they are registered users). –  KajMagnus Jul 15 '12 at 2:38
    
@fdsa Yes a cookie would probably be needed regardless, in case [someone who has not commented before (so name+email is unknown)] submits a comment rating. Then I'd still like that user to have his/her rating highlighted –  KajMagnus Jul 15 '12 at 2:40
    
@fdsa "however users might not like still being half logged in" — that's a good point. What if they don't realize that the system highlights their ratings, because of a cookie, and then they leaves the computer. What if it's in a public library — then someone else could find out how they've rated other's comments. –  KajMagnus Jul 15 '12 at 2:44

1 Answer 1

up vote 1 down vote accepted

An unregistered user's name and email are available in cookies, once s/he has left a comment. The cookies are named like:

comment_author_3e6aea64ec43ed661a5dee3433f1e8bc
comment_author_email_3e6aea64ec43ed661a5dee3433f1e8bc

You can retrieve them like so:

sanitize_comment_cookies();
wp_get_current_commenter(); // returns comment_author,
                            //   comment_author_email, comment_author_url

They're created here in comment.php:

/**
 * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
 * to recall previous comments by this commentator that are still held in moderation.
 *
 * @param object $comment Comment object.
 * @param object $user Comment author's object.
 *
 * @since 3.4.0
 */
function wp_set_comment_cookies($comment, $user) {
    if ( $user->exists() )
        return;

    $comment_cookie_lifetime = apply_filters('comment_cookie_lifetime', 30000000);
    setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, ...);
    setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, ...);
    setcookie('comment_author_url_' . COOKIEHASH, ...);
}

(I found out by stepping through wp-comments-post.php with a debugger.)

((As mentioned in the comments below the original question above, couldn't this sometimes be a privacy issue, if someone posts a comment in a public library — then the one who uses the computer afterwards might find those comments that are "waiting for authentication", and know who wrote them, hmm))

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.