今天小編在某 WordPress 網站註冊了個帳號,在完成註冊後該網站系統傳送來一封預設的註冊提醒通知郵件,郵件樣式如下圖:

當時小編就想到 WordPress 預設的新使用者註冊提醒郵件的樣式也未免太破了吧?正好無聊就琢磨了下 WordPress 的新使用者郵件通知的函式。發現在 wp-includes/pluggable.php 檔案有個 wp_new_user_notification() 函式是用來定義新使用者郵件通知的。小編根據檔案琢磨了下發現可以用以下方式來定義郵件樣式:

<?php
/*
  Plugin Name: new-user-notification
  Description: 重新定義傳送郵件的內容和格式
  Version: 1.0
 */

if ( !function_exists('wp_new_user_notification') ) :
/**
 * Notify the blog admin of a new user, normally via email.
 *
 * @since 2.0
 *
 * @param int $user_id User ID
 * @param string $plaintext_pass Optional. The user's plaintext password
 */
function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $user = get_userdata( $user_id );

    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);

    // 獲取部落格名稱
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    // 給管理員傳送的郵件內容,這裡是 HTML 格式
    $message = '<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title> 新使用者註冊</title>
    </head>
    <body>
    <div align="center">
    <table cellpadding="0" cellspacing="1" style="border:3px solid #d9e9f1;background:#7fbddd; text-align:left;">
    <tr>
    <td style="padding:0;">
    <table cellpadding="30" cellspacing="0" style="border:1px solid #ffffff;background:#f7f7f7;width:500px;">
    <tr><td style="line-height:2;font-size:12px;"> 您的網站 <strong>' . $blogname . '</strong>
    有新使用者註冊。<br /> 使用者名稱:' . $user_login . '<br />
    Email:' . $user_email . '<br />
    <br /> 如果您不是  <strong>' . $blogname . '</strong> 的管理員,請直接忽略本郵件!</div>
    </td></tr></table></td></tr></table></div></body></html>';

    // 給網站管理員傳送郵件
    $message_headers = "Content-Type: text/html; charset="utf-8"
";
    @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message, $message_headers);

    if ( empty($plaintext_pass) )
        return;

    // 你可以在此修改傳送給新使用者的通知 Email,這裡是 HTML 格式
    $message = '<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title> 新使用者註冊</title>
    </head>
    <body>
    <div align="center">
    <table cellpadding="0" cellspacing="1" style="border:3px solid #d9e9f1;background:#7fbddd; text-align:left;">
    <tr><td style="padding:0;">
    <table cellpadding="30" cellspacing="0" style="border:1px solid #ffffff;background:#f7f7f7;width:500px;">
    <tr>
    <td style="line-height:2;font-size:12px;"> 您剛剛在網站 <strong>' . $blogname . '</strong> 註冊一個帳號。<br />
    使用者名稱:' . $user_login . '<br /> 登入密碼:' . $plaintext_pass . '<br /> 登入網址:<a href="'%20.%20wp_login_url()%20.%20'">'%20.%20wp_login_url()%20.%20'</a><br />
    <br /> 如果您沒有在 <strong>'. $blogname . '</strong> 註冊過任何資訊,請直接忽略本郵件!</div>
    </td></tr></table></td></tr></table></div>
    </body>
    </html>';

    // sprintf(__('[%s] Your username and password'), $blogname) 為郵件標題
    wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message, $message_headers);
}
endif;

?>

以上程式碼的加到主題的 functions.php 或者命名為 wxd_new-user.php 檔案然後把檔案傳到 wp-content/plugins/資料夾下,再到後臺啟用一個名為 new-user-notification 的外掛即可。

PS:程式碼參考露兜