author_email
云策文档标注
概述
author_email 是 WordPress 中的一个过滤器,用于修改评论作者邮箱地址的显示。它允许开发者通过回调函数自定义邮箱地址的输出,适用于隐私保护、条件修改等多种场景。
关键要点
- 过滤器名称:author_email,用于过滤评论作者的邮箱地址。
- 参数:$comment_author_email(评论作者邮箱地址字符串)和 $comment_id(评论 ID 字符串)。
- 用途:可修改邮箱地址,例如固定值、基于评论 ID 或用户角色的条件修改。
- 版本历史:从 WordPress 1.2.0 引入,4.1.0 版本添加了 $comment_id 参数。
代码示例
// 示例1:简单修改邮箱地址为固定值
function wpdocs_custom_author_email( $comment_author_email, $comment_id ) {
return 'customemail@example.com';
}
add_filter( 'author_email', 'wpdocs_custom_author_email', 10, 2 );
// 示例2:基于评论 ID 的条件修改
function wpdocs_conditional_author_email( $comment_author_email, $comment_id ) {
if ( 123 === $comment_id ) {
return 'specialemail@example.com';
}
return $comment_author_email;
}
add_filter( 'author_email', 'wpdocs_conditional_author_email', 10, 2 );
// 示例3:基于用户角色的条件修改
function wpdocs_conditional_comment_author_email( $comment_author_email, $comment_id ) {
$comment = get_comment( $comment_id );
if ( user_can( $comment->user_id, 'administrator' ) ) {
return 'admin@example.com';
}
return $comment_author_email;
}
add_filter( 'author_email', 'wpdocs_conditional_comment_author_email', 10, 2 );注意事项
- 使用 add_filter 时需指定优先级(如 10)和参数数量(如 2)。
- 修改邮箱地址可能影响评论功能,如通知发送,请谨慎操作。
- 建议在子主题的 functions.php 文件中添加过滤器代码,以避免主题更新时丢失。
原文内容
Filters the comment author’s email for display.
Parameters
$comment_author_emailstring-
The comment author’s email address.
$comment_idstring-
The comment ID as a numeric string.
Source
echo apply_filters( 'author_email', $comment_author_email, $comment->comment_ID );
Skip to note 2 content
Noruzzaman
If you add the following to the functions.php file of your child theme, you can modify the author’s email using the following instructions:
Example 1: Simple Email Modification
In this example, we’ll add a filter that modifies the email address of the comment author to a fixed value:
function wpdocs_custom_author_email( $comment_author_email, $comment_id ) { // Modify the email address return 'customemail@example.com'; } add_filter( 'author_email', 'wpdocs_custom_author_email', 10, 2 );Example 2: Conditionally Modifying the Email Address
In this example, we’ll modify the email address based on the comment ID:
function wpdocs_conditional_author_email( $comment_author_email, $comment_id ) { if ( 123 === $comment_id ) { // Modify the email address for comment ID 123 return 'specialemail@example.com'; } // Return the original email address for other comments return $comment_author_email; } add_filter( 'author_email', 'wpdocs_conditional_author_email', 10, 2 );Example 3: Another Conditionally Modifying the Email Address:
Change the email based on certain conditions, such as the comment author’s role.
function wpdocs_conditional_comment_author_email( $comment_author_email, $comment_id ) { $comment = get_comment( $comment_id ); // Check if the author is an admin if ( user_can( $comment->user_id, 'administrator' ) ) { return 'admin@example.com'; } return $comment_author_email; } add_filter( 'author_email', 'wpdocs_conditional_comment_author_email', 10, 2 );The
author_emailfilter in WordPress provides a flexible way to modify comment author email addresses. By creating callback functions and hooking them into the filter, you can customize how these email addresses are handled on your website. Whether it’s for privacy, conditional changes, or logging purposes, theauthor_emailfilter can be utilized in various ways to meet your needs.