WordPress как на ладони

wp_get_current_commenter()WP 2.0.4

Получает имя, почту, URL текущего комментатора из куков. Используется для формы комментариев.

Данные берутся из куков, которые добавляются неавторизованным пользователям при комментировании.

Ожидает, что данные в куках уже очищены. Иногда есть смысл проверить вернувшиеся данные.

Хуки из функции

Возвращает

Массив. Массив данных в виде:

Array(
	[comment_author]       => Kama
	[comment_author_email] => fooo@gmail.com
	[comment_author_url]   => https://example.com/mypage
)

Использование

wp_get_current_commenter();

Примеры

0

#1 Демонстрация того, что получает функция

Получим значения полей формы комментирования для неавторизованного пользователя.

$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
)
*/
0

#2 Вывод полей формы комментирования

Этот пример показывает как вывести поля: Имя, Почта и Сайт с предварительным заполнением данных если они есть в куках:

// получим данные из куков
$commenter = wp_get_current_commenter();

// определим поля которые нужно вывести
$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">Имя' . $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">Сайт</label> ' .
				'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" maxlength="200" /></p>',
];

// выводим имеющиеся поля
foreach( $fields as $field ){
	echo $field;
}

Заметки

Список изменений

С версии 2.0.4 Введена.

Код wp_get_current_commenter() WP 6.5.2

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' ) );
}
5 комментариев
    Войти