如何修改WordPress自动创建用户时发送的注册欢迎邮件内容?
解决WordPress自定义自动创建客户账户邮件内容的问题
我完全懂你现在的困扰——默认的账户创建邮件太简陋,常用的通知插件又找不到对应的设置项。别担心,我们可以通过代码钩子来完全自定义这封邮件,以下是具体步骤:
第一步:确定邮件的触发来源
首先需要确认这封邮件是WordPress默认机制发送的,还是你的发票录入功能自定义触发的。可以先添加一段调试代码到主题的functions.php(或自定义插件)里,拦截所有邮件并记录信息:
add_action('wp_mail', function($atts) { // 将邮件详情写入调试日志,方便分析触发来源 error_log('邮件调试信息: ' . print_r($atts, true)); return $atts; });
触发一次账户创建操作后,打开wp-content/debug.log文件(需先在wp-config.php中开启WP_DEBUG_LOG),查看这封邮件的主题、内容和头部信息,确认它的触发逻辑。
第二步:根据触发来源自定义邮件
情况1:使用WordPress默认新用户通知
如果日志显示这是默认的新用户通知邮件,我们可以用wp_new_user_notification_email过滤器直接修改内容:
add_filter('wp_new_user_notification_email', 'custom_customer_account_email', 10, 3); function custom_customer_account_email($email_data, $user, $blogname) { // 自定义邮件主题 $email_data['subject'] = __('Your Access Credentials | ' . $blogname); // 提取自动生成的密码(默认邮件的message字段包含密码) preg_match('/Password: ([^\s]+)/', $email_data['message'], $password_match); $password = $password_match[1] ?? ''; // 构建你的自定义HTML邮件内容 $login_url = esc_url(home_url('/my-account')); $user_email = $user->user_email; $home_url = esc_url(home_url()); $message = '<p>Dear customer,</p>'; $message .= '<p>we have set an account for you on <span style="font-weight: bold;"><a href="' . $home_url . '">' . $blogname . '</a></span>.</p>'; $message .= '<p>You can use this account to update your information.</p>'; $message .= '<div style="width: 90%; margin-left: 5%; background-color: #edf2f7; text-align: center; padding: 15px 0;">'; $message .= '<p>Your Login Credentials are:</p>'; $message .= '<p><span style="font-weight: bold;">Login:</span> ' . $user_email . '</p>'; $message .= '<p><span style="font-weight: bold;">Password:</span> ' . $password . '</p>'; $message .= '</div>'; $message .= '<p>To access your account and update your password you can login <a href="' . $login_url . '">here</a></p>'; $message .= '<p>Kind Regards</p>'; $message .= '<p>' . $blogname . ' Accounts</p>'; $email_data['message'] = $message; // 设置邮件为HTML格式 $email_data['headers'][] = 'Content-Type: text/html; charset=UTF-8'; return $email_data; }
情况2:自定义触发的邮件(比如发票录入功能)
如果这封邮件是你自己的代码触发的(例如直接调用wp_mail发送),我们可以用wp_mail钩子精准拦截并修改:
add_filter('wp_mail', 'custom_invoice_account_email', 10, 1); function custom_invoice_account_email($mail_atts) { // 通过邮件主题判断是否是目标邮件 if (strpos($mail_atts['subject'], 'Your Access Credentials') !== false) { // 替换主题为自定义内容 $mail_atts['subject'] = 'Your Access Credentials | ' . get_bloginfo('name'); // 从原邮件内容中提取登录邮箱和密码 preg_match('/Login: ([^\s]+)/', $mail_atts['message'], $login_match); preg_match('/Password: ([^\s]+)/', $mail_atts['message'], $password_match); $user_email = $login_match[1] ?? ''; $password = $password_match[1] ?? ''; // 构建你的自定义HTML模板 $login_url = esc_url(home_url('/my-account')); $blogname = get_bloginfo('name'); $home_url = esc_url(home_url()); $message = '<p>Dear customer,</p>'; $message .= '<p>we have set an account for you on <span style="font-weight: bold;"><a href="' . $home_url . '">' . $blogname . '</a></span>.</p>'; $message .= '<p>You can use this account to update your information.</p>'; $message .= '<div style="width: 90%; margin-left: 5%; background-color: #edf2f7; text-align: center; padding: 15px 0;">'; $message .= '<p>Your Login Credentials are:</p>'; $message .= '<p><span style="font-weight: bold;">Login:</span> ' . $user_email . '</p>'; $message .= '<p><span style="font-weight: bold;">Password:</span> ' . $password . '</p>'; $message .= '</div>'; $message .= '<p>To access your account and update your password you can login <a href="' . $login_url . '">here</a></p>'; $message .= '<p>Kind Regards</p>'; $message .= '<p>' . $blogname . ' Accounts</p>'; $mail_atts['message'] = $message; // 添加HTML格式头部 $mail_atts['headers'][] = 'Content-Type: text/html; charset=UTF-8'; } return $mail_atts; }
第三步:测试与优化
- 将代码添加到主题的
functions.php文件(或创建一个简单的自定义插件,避免主题更新丢失代码) - 创建一个测试客户账户,检查邮件是否正确发送,样式和链接是否正常
- 如果需要调整内容或样式,直接修改代码中的HTML部分即可
至于为什么Better Notifications for WordPress插件找不到这个设置?因为这封邮件可能是你的发票系统自定义触发的,不属于插件预设覆盖的默认邮件类型,所以需要用代码钩子来实现自定义。
内容的提问来源于stack exchange,提问作者SupGen
相关产品推荐
相关产品推荐

