How to get the comment author name, email and website from his cookies?

I’m sure there are a lot of things where this function could be helpful. As for me I wanted the following: someone posts a comment on the website and when he goes to contact me page, the respond form would be filled with this data automatically . I’ve prepared a function for you which returns […]

January 25, 2016

I’m sure there are a lot of things where this function could be helpful. As for me I wanted the following: someone posts a comment on the website and when he goes to contact me page, the respond form would be filled with this data automatically

.

I’ve prepared a function for you which returns the array with all of the comment author data: his name, his email and his website (if specified of course).

You can insert this function to your current WordPress theme functions.php file.

function rudr_get_comment_author_data() {
	if( is_user_logged_in() )
		return false; // return false and exit the function if user is logged in
 
	$comment_author = array();
 
	foreach( $_COOKIE as $cookie_key => $cookie_value ) { // loop through all the cookies
 
		if ( strpos( ' ' . $cookie_key, 'comment_author_email' ) > 0 ) { // is email matches
        		$comment_author['email'] = urldecode( $cookie_value );
        	} elseif ( strpos( ' ' . $cookie_key, 'comment_author_url' ) > 0 ) { // if URL matches
        		$comment_author['url'] = urldecode( $cookie_value );
        	} elseif ( strpos( ' ' . $cookie_key, 'comment_author' ) > 0 ) { // else if author name
       			$comment_author['name'] = urldecode( $cookie_value );
       		}
 
	}
	return $comment_author; // the result array
}

Some notices:

  • At the beginning the function checks the user if he is a registered user, if yes the function returns false. Of course we can complete the function with the code for registered users, but this is another story.
  • The second thing the function do is looping though all cookies keys and checking them for comment_author_email, comment_author_url and comment_author strings. Please, notice, that checking comment_author should the last check.
  • The functions returns an array like Array ( [name] => [email] => [url] => ) or Array ( [name] => [email] => ) if the website field is not filled.

Let’s look at the exaple with the respond form I was talking before.

<?php
	$author = rudr_get_comment_author_data();
?>
<form action="http://truemisha.ru" method="POST">
	<input type="text" name="name" value="<?php if( isset( $author['name'] ) ) echo $author['name'] ?>" />
	<input type="email" name="email" value="<?php if( isset( $author['email'] ) ) echo $author['email'] ?>" />
	<textarea name="msg"></textarea>
	<button>Submit it</button>
</form>

The isset() is needed to avoid PHP notices like this Notice: Undefined index:. You can see them if WP_DEBUG=true in wp-config.php and one of the array elements is missing (comment author website for example).

Only the best of WordPress

once a week, no spam

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email