wp_get_current_commenter()WP 2.0.4

Get current commenter's name, email, URL from cookies. Used for comments form.

This data is retrieved from the cookies. The cookies are added when unauthorized user leave a comment for a post.

Expects the cookies data has already been sanitized. Sometimes it makes sense to check the retrieved data.

Hooks from the function

Return

Array. Comment author, email, URL like this:

Array(
	[comment_author]       => Kama
	[comment_author_email] => [email protected]
	[comment_author_url]   => https://example.com/mypage
)

Usage

wp_get_current_commenter();

Examples

0

#1 Return values example

Get the comment form fields values for the unauthorized user.

$commenter = wp_get_current_commenter();

print_r( $commenter );
/*
Array(
	[comment_author]       => Kama
	[comment_author_email] => [email protected]
	[comment_author_url]   => https://example.com/mypage
)
*/
0

#2 Display a form with the commenter's data

This example shows how to display fields Name, Email, Site with the actual data of the commenter:

// get the data
$commenter = wp_get_current_commenter();

// define the fields
$req = get_option( 'require_name_email' ) ? ' <span class="required">*</span>' : '';

$aria_req = $req ? " aria-required='true'" : '';
$html_req = $req ? " required='required'"  : '';

$fields =  [
	'author' => '<p class="comment-form-author">' . '<label for="author">Name' . $req . '</label> ' .
				'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" maxlength="245"' . $aria_req . $html_req . ' /></p>',
	'email'  => '<p class="comment-form-email"><label for="email">Email' . $req . '</label> ' .
				'<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" maxlength="100" aria-describedby="email-notes"' . $aria_req . $html_req  . ' /></p>',
	'url'    => '<p class="comment-form-url"><label for="url">Site</label> ' .
				'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" maxlength="200" /></p>',
];

// display the fields
foreach( $fields as $field ){
	echo $field;
}

Notes

Changelog

Since 2.0.4 Introduced.

wp_get_current_commenter() code WP 6.4.3

function wp_get_current_commenter() {
	// Cookies should already be sanitized.

	$comment_author = '';
	if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) {
		$comment_author = $_COOKIE[ 'comment_author_' . COOKIEHASH ];
	}

	$comment_author_email = '';
	if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
		$comment_author_email = $_COOKIE[ 'comment_author_email_' . COOKIEHASH ];
	}

	$comment_author_url = '';
	if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) {
		$comment_author_url = $_COOKIE[ 'comment_author_url_' . COOKIEHASH ];
	}

	/**
	 * Filters the current commenter's name, email, and URL.
	 *
	 * @since 3.1.0
	 *
	 * @param array $comment_author_data {
	 *     An array of current commenter variables.
	 *
	 *     @type string $comment_author       The name of the current commenter, or an empty string.
	 *     @type string $comment_author_email The email address of the current commenter, or an empty string.
	 *     @type string $comment_author_url   The URL address of the current commenter, or an empty string.
	 * }
	 */
	return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) );
}