wp_get_current_commenter()
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
#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 ) */
#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. |