函数文档

wp_get_current_commenter()

💡 云策文档标注

概述

wp_get_current_commenter() 函数用于获取当前评论者的姓名、邮箱和 URL 信息,基于已清理的 Cookie 数据。开发者需注意返回数组的验证。

关键要点

  • 函数返回一个包含 comment_author、comment_author_email 和 comment_author_url 键的数组,值可能为空字符串
  • 依赖 Cookie 数据,需确保已使用 sanitize_comment_cookies() 进行清理
  • 包含 wp_get_current_commenter 过滤器,允许自定义返回的评论者数据
  • 与 comment_form() 等函数相关,常用于评论模板中

代码示例

$current_commenter = wp_get_current_commenter();
// 输出示例数组
Array
(
    [comment_author] => John Doe
    [comment_author_email] => johndoe@gmail.com
    [comment_author_url] => http://example.com
)

注意事项

函数假设 Cookie 已清理,开发者应重新检查返回数组的有效性以确保安全。


📄 原文内容

Gets current commenter’s name, email, and URL.

Description

Expects cookies content to already be sanitized. User of this function might wish to recheck the returned array for validity.

See also

Return

array An array of current commenter variables.

  • comment_author string
    The name of the current commenter, or an empty string.
  • comment_author_email string
    The email address of the current commenter, or an empty string.
  • comment_author_url string
    The URL address of the current commenter, or an empty string.

Source

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' ) );
}

Hooks

apply_filters( ‘wp_get_current_commenter’, array $comment_author_data )

Filters the current commenter’s name, email, and URL.

Changelog

Version Description
2.0.4 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Get current commenter detail

    $current_commenter = wp_get_current_commenter();

    Output:

    Array
    (
        [comment_author] => John Doe
        [comment_author_email] => <a href="mailto:johndoe@gmail.com">johndoe@gmail.com</a>
        [comment_author_url] => <a href="http://example.com" rel="nofollow ugc">http://example.com</a>
    )