WordPress at a glance

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] => fooo@gmail.com
	[comment_author_url]   => https://example.com/mypage
)

Usage

wp_get_current_commenter();

Examples

#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] => fooo@gmail.com
	[comment_author_url]   => https://example.com/mypage
)
*/

#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

  • See: sanitize_comment_cookies() Use to sanitize cookies

Changelog

Since 2.0.4 Introduced.

Code of wp get current commenter: wp-includes/comment.php WP 5.2.2

<?php
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 author of the comment. Default empty.
	 *     @type string $comment_author_email The email address of the `$comment_author`. Default empty.
	 *     @type string $comment_author_url   The URL address of the `$comment_author`. Default empty.
	 * }
	 */
	return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) );
}

Related Functions

From tag: comment_cookies

More from category: Comments

No comments
    Hello, !     Log In . Register