get_comment_author_IP()
云策文档标注
概述
get_comment_author_IP() 函数用于获取指定评论作者的 IP 地址。它接受一个可选的评论 ID 或 WP_Comment 对象作为参数,并返回 IP 地址字符串,若不可用则返回空字符串。
关键要点
- 函数参数:$comment_id(可选),可以是整数或 WP_Comment 对象,默认使用当前评论。
- 返回值:字符串类型的评论作者 IP 地址,若无则返回空字符串。
- 核心功能:通过 get_comment() 获取评论对象,并应用 get_comment_author_IP 过滤器以允许自定义 IP 地址返回。
- 版本更新:从 4.4.0 版本起,$comment_id 参数支持 WP_Comment 对象;函数自 1.5.0 版本引入。
代码示例
function get_comment_author_IP( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment );
}注意事项
- 函数名遵循 WordPress 命名规范,但包含非标准大写(如 IP),使用时需注意兼容性。
- 过滤器 get_comment_author_IP 允许开发者修改返回的 IP 地址,参数包括 IP 地址、评论 ID 和评论对象。
- 相关函数包括 get_comment() 用于获取评论数据,以及 comment_author_IP() 用于直接显示 IP 地址。
原文内容
Retrieves the IP address of the author of the current comment.
Parameters
$comment_idint|WP_Commentoptional-
WP_Comment or the ID of the comment for which to get the author’s IP address.
Default current comment.
Source
function get_comment_author_IP( $comment_id = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
$comment = get_comment( $comment_id );
/**
* Filters the comment author's returned IP address.
*
* @since 1.5.0
* @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
*
* @param string $comment_author_ip The comment author's IP address, or an empty string if it's not available.
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment object.
*/
return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
}
Hooks
- apply_filters( ‘get_comment_author_IP’, string $comment_author_ip, string $comment_id, WP_Comment $comment )
-
Filters the comment author’s returned IP address.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability for $comment_id to also accept a WP_Comment object. |
| 1.5.0 | Introduced. |