views:

33

answers:

2

This deals with WordPress plugin development. Even if a user has not actually signed into WordPress, but has left a comment, WordPress remembers them from the last time. I wanted to toggle showing a "Subscribe To Newsletter" checkbox if they had already subscribed previously to a newsletter. I've already worked out the checkbox and it writes to another table storing the comment author's email when they post. But what is difficult is that I want to not show the checkbox if the user has already subscribed. It's easy enough to not show the checkbox if I had an actual email address, but I don't know how to retrieve the email address simply when the comment form is shown.

Note, using get_currentuserinfo() does not apply here because the user is a commentor, not a blog article admin or author.

I need something where the user comes to a post they have not yet commented on. They may have commented on other posts, just not this one. In WordPress, you'll notice that it automatically knows this and assigns a Name and Mail field value through cookies.

A: 

Here's a routine I have worked out, but perhaps you can do it better through something in the WordPress codex?

function getCommentAuthorEmail() {
global $user_level;

  get_currentuserinfo();
  if($user_level > 0) { //if signed into wordpress as admin or author
    return ''; //don't provide anything
  }

  $sEmail = '';
  try {
    foreach($_COOKIE as $sKey => $sVal) {
      if (strpos(' ' . $sKey, 'comment_author_email')>0) {
        $sEmail = urldecode($sVal);
        break;
      }
    }
  } catch (Exception $e){}
  return $sEmail;
}
Volomike
+1  A: 

You might be able to do that using get_comments, it returns an array with the key comment_author_email.

Basically you'd have to loop through all the comments to all the posts, retrieve all emails and then check it against your newsletter data.

More information about the syntax (and possible alternatives) can be found here: http://codex.wordpress.org/Function_Reference/get_comments

Björn
Ah, perhaps I didn't make myself clear. This won't work because I need something where the user comes to a post they have not yet commented on. They may have commented on other posts, just not this one. In WordPress, you'll notice that it automatically knows this and assigns a Name and Mail field value through cookies.
Volomike