钩子文档

retrieve_password_notification_email

💡 云策文档标注

概述

retrieve_password_notification_email 是一个 WordPress 过滤器钩子,用于自定义发送给用户的密码重置通知邮件内容。它允许开发者修改邮件的收件人、主题、正文和头部等参数。

关键要点

  • 过滤器钩子:retrieve_password_notification_email,用于过滤密码重置通知邮件的参数。
  • 参数:$defaults(数组,包含 to、subject、message、headers 等键值),$key(激活密钥),$user_login(用户名),$user_data(WP_User 对象)。
  • 用途:在 retrieve_password() 函数中调用,处理密码找回邮件的发送。
  • 版本:从 WordPress 6.0.0 版本引入。

代码示例

add_filter( 'retrieve_password_notification_email', 'wpdocs_reset_password_message', 99, 4 );
function wpdocs_reset_password_message( $defaults, $key, $user_login, $user_data ) {
    $password_reset_link = esc_url( network_site_url( "wp-login.php?action=rp&key={$key}&login=" . rawurlencode( $user_login ), 'login' ) );
    $display_name = sprintf(
        '%3$s (%2$s)',
        esc_url( get_edit_profile_url( $user_data->ID ) ),
        esc_html( $user_data->user_email ),
        esc_html( $user_data->display_name )
    );

    $defaults['message'] = "The password for the following account has been requested to be reset:

    {$display_name}

    If this was a mistake, just ignore this email and nothing will happen.

    To reset your password, visit the following address:

    {$password_reset_link}";

    return $defaults;
}

注意事项

  • $defaults 是一个数组,包含 to、subject、message、headers 等键,可直接修改这些值来自定义邮件内容。
  • 使用此过滤器时,需确保正确处理用户数据和链接,以避免安全风险。

📄 原文内容

Filters the contents of the reset password notification email sent to the user.

Parameters

$defaultsarray
The default notification email arguments. Used to build wp_mail() .

  • to string
    The intended recipient – user email address.
  • subject string
    The subject of the email.
  • message string
    The body of the email.
  • headers string
    The headers of the email.

$keystring
The activation key.
$user_loginstring
The username for the user.
$user_dataWP_User
WP_User object.

Source

$notification_email = apply_filters( 'retrieve_password_notification_email', $defaults, $key, $user_login, $user_data );

Changelog

Version Description
6.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Here’s an example of how you can use this inside functions.php:

    add_filter( 'retrieve_password_notification_email', 'wpdocs_reset_password_message', 99, 4 );
    function wpdocs_reset_password_message( $defaults, $key, $user_login, $user_data ) {
    	$password_reset_link = esc_url( network_site_url( "wp-login.php?action=rp&key;={$key}&login;=" . rawurlencode( $user_login ), 'login' ) );
    	$display_name = sprintf(
    		'<a href="%1$s" rel="nofollow ugc">%3$s</a> (%2$s)',
    		esc_url( get_edit_profile_url( $user_data->ID ) ),
    		esc_html( $user_data->user_email ),
    		esc_html( $user_data->display_name )
    	);
    
    	$defaults['message'] = "The password for the following account has been requested to be reset:
    
    	{$display_name}
    
    	If this was a mistake, just ignore this email and nothing will happen.
    
    	To reset your password, visit the following address:
    
    	{$password_reset_link}";
    
    	return $defaults;
    }

    Inside the $defaults variable is an array with all the necessary data that you can modify.

    $defaults = array(
         'to'      => 'example@email.com',
         'subject' => 'Some subject...',
         'message' => 'Email message...',
         'headers' => '',
    );