WordPress 對新用戶註冊有一個歡迎郵件,一般告知用戶的用戶名和密碼,以及登錄的地址;還有一封發給管理員的通知郵件,告訴管理員新用戶的用戶名和郵箱。這兩封郵件的內容一般都比較簡單,今天我們就一起來學習一下自定義這兩個郵件。
首先,我們需要瞭解一下 WordPress 用來發送這兩個郵件的函數:
1 |
<?php wp_new_user_notification( $user_id, $plaintext_pass ) ?> |
<?php wp_new_user_notification( $user_id, $plaintext_pass ) ?>
默認的郵件代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// Redefine user notification function if ( !function_exists('wp_new_user_notification') ) { function wp_new_user_notification( $user_id, $plaintext_pass = '' ) { $user = new WP_User( $user_id ); $user_login = stripslashes( $user->user_login ); $user_email = stripslashes( $user->user_email ); $message = sprintf( __('New user registration on %s:'), get_option('blogname') ) . "\r\n\r\n"; $message .= sprintf( __('Username: %s'), $user_login ) . "\r\n\r\n"; $message .= sprintf( __('E-mail: %s'), $user_email ) . "\r\n"; @wp_mail( get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname') ), $message ); if ( empty( $plaintext_pass ) ) return; $message = __('Hi there,') . "\r\n\r\n"; $message .= sprintf( __("Welcome to %s! Here's how to log in:"), get_option('blogname')) . "\r\n\r\n"; $message .= wp_login_url() . "\r\n"; $message .= sprintf( __('Username: %s'), $user_login ) . "\r\n"; $message .= sprintf( __('Password: %s'), $plaintext_pass ) . "\r\n\r\n"; $message .= sprintf( __('If you have any problems, please contact me at %s.'), get_option('admin_email') ) . "\r\n\r\n"; $message .= __('Adios!'); wp_mail( $user_email, sprintf( __('[%s] Your username and password'), get_option('blogname') ), $message ); } } |
// Redefine user notification function
if ( !function_exists(‘wp_new_user_notification’) ) { function wp_new_user_notification( $user_id, $plaintext_pass = ” ) { $user = new WP_User( $user_id ); $user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email ); $message = sprintf( __(‘New user registration on %s:’), get_option(‘blogname’) ) . "\r\n\r\n";
$message .= sprintf( __(‘Username: %s’), $user_login ) . "\r\n\r\n";
$message .= sprintf( __(‘E-mail: %s’), $user_email ) . "\r\n"; @wp_mail(
get_option(‘admin_email’),
sprintf(__(‘[%s] New User Registration’), get_option(‘blogname’) ),
$message
); if ( empty( $plaintext_pass ) )
return; $message = __(‘Hi there,’) . "\r\n\r\n";
$message .= sprintf( __("Welcome to %s! Here’s how to log in:"), get_option(‘blogname’)) . "\r\n\r\n";
$message .= wp_login_url() . "\r\n";
$message .= sprintf( __(‘Username: %s’), $user_login ) . "\r\n";
$message .= sprintf( __(‘Password: %s’), $plaintext_pass ) . "\r\n\r\n";
$message .= sprintf( __(‘If you have any problems, please contact me at %s.’), get_option(‘admin_email’) ) . "\r\n\r\n";
$message .= __(‘Adios!’); wp_mail(
$user_email,
sprintf( __(‘[%s] Your username and password’), get_option(‘blogname’) ),
$message
);
}
}
這個函數允許通過外掛來覆蓋,所以我們必須創建一個新外掛來覆蓋WP自帶的郵件。下面是一個示例,在這個例子中,我們不僅修改郵件內容,而且讓郵件支援發送 html 內容,這樣就可以更加靈活地拓展郵件的展示效果啦!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php /* Plugin Name: WPDX New User Notification Description:重新定義新用戶註冊郵件 Version: 1.0 */ if ( !function_exists('wp_new_user_notification') ) { function wp_new_user_notification( $user_id, $plaintext_pass = '' ) { $user = new WP_User($user_id); //獲取用戶名和郵箱 $user_login = stripslashes($user->user_login); $user_email = stripslashes($user->user_email); //獲取管理員郵箱和部落格名稱 $admin_email = stripslashes(get_option('admin_email')); $blog_name = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); //自定義管理員郵件 $headers = 'From: '.$blog_name.' <'.$admin_email.'>'; $headers .= 'MIME-Version: 1.0'; $headers .= 'Content-type: text/html; charset=uft-8'; $headers .= 'Content-Transfer-Encoding: 8bit'; $message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div>'; $message .= '<p>'.sprintf(__('你的網站[%s] 有新用戶註冊:'), $blog_name ) . '</p>'; $message .= '<p>'.sprintf(__('Username: %s'), $user_login) . '</p>'; $message .= '<p>'.sprintf(__('E-mail: %s'), $user_email) . '</p></div></body></html>'; @wp_mail($admin_email, sprintf(__('[%s] New User Registration'), $blog_name), $message, $headers); //判斷新用戶密碼是否為空 if ( empty($plaintext_pass) ) return; //自定義新用戶歡迎郵件 $headers = 'From: '.$blog_name.' <'.$admin_email.'>'; $headers .= 'MIME-Version: 1.0'; $headers .= 'Content-type: text/html; charset=uft-8'; $headers .= 'Content-Transfer-Encoding: 8bit'; $message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div>'; $message .= '<p>'.__('親愛的:') .$user_login. '</p>'; $message .= '<p>這是由大偉吉他教室(www.daweijita.com)發來的郵件。</p>'; $message .= '<p>恭喜您今天正式成為教室的一員,我們已經展開熱情的懷抱歡迎你。這裡有專業的吉他老師,多得好幾年都學不完的教學,還有來天南地北的小夥伴。音樂的旅程即將開啟,你準備好瞭麼?</p>'; $message .= '<p>您的賬號信息如下:</p>'; $message .= '<p>'.sprintf(__('Username: %s'), $user_login) . '</p>'; $message .= '<p>'.sprintf(__('Password: %s'), $plaintext_pass) . '</p>'; $message .= '<p>請妥善保存好自己的賬戶信息,如果您忘記密碼,可以通過登錄窗口的密碼找回功能找回密碼。</p>'; $message .= '<p>加入我們的微信,可及時獲取最新的教學內容!</p>'; $message .= '<p><img src="http://cdn.daweijita.com/2014/07/dwjt-wx.jpg" /></p></div></body></html>'; @wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blog_name), $message , $headers); } //讓郵件支援html add_filter( 'wp_mail_content_type', 'wpdx_set_html_content_type' ); function wpdx_set_html_content_type() { return 'text/html'; } } |
<?php
/*
Plugin Name: WPDX New User Notification
Description:重新定義新用戶註冊郵件
Version: 1.0
*/
if ( !function_exists(‘wp_new_user_notification’) ) {
function wp_new_user_notification( $user_id, $plaintext_pass = ” ) {
$user = new WP_User($user_id);
//獲取用戶名和郵箱
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
//獲取管理員郵箱和部落格名稱
$admin_email = stripslashes(get_option(‘admin_email’));
$blog_name = wp_specialchars_decode(get_option(‘blogname’), ENT_QUOTES);
//自定義管理員郵件
$headers = ‘From: ‘.$blog_name.’ <‘.$admin_email.’>’;
$headers .= ‘MIME-Version: 1.0’;
$headers .= ‘Content-type: text/html; charset=uft-8’;
$headers .= ‘Content-Transfer-Encoding: 8bit’; $message = ‘<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div>’;
$message .= ‘<p>’.sprintf(__(‘你的網站[%s] 有新用戶註冊:’), $blog_name ) . ‘</p>’;
$message .= ‘<p>’.sprintf(__(‘Username: %s’), $user_login) . ‘</p>’;
$message .= ‘<p>’.sprintf(__(‘E-mail: %s’), $user_email) . ‘</p></div></body></html>’; @wp_mail($admin_email, sprintf(__(‘[%s] New User Registration’), $blog_name), $message, $headers);
//判斷新用戶密碼是否為空
if ( empty($plaintext_pass) )
return;
//自定義新用戶歡迎郵件
$headers = ‘From: ‘.$blog_name.’ <‘.$admin_email.’>’;
$headers .= ‘MIME-Version: 1.0’;
$headers .= ‘Content-type: text/html; charset=uft-8’;
$headers .= ‘Content-Transfer-Encoding: 8bit’; $message = ‘<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div>’;
$message .= ‘<p>’.__(‘親愛的:’) .$user_login. ‘</p>’;
$message .= ‘<p>這是由大偉吉他教室(www.daweijita.com)發來的郵件。</p>’;
$message .= ‘<p>恭喜您今天正式成為教室的一員,我們已經展開熱情的懷抱歡迎你。這裡有專業的吉他老師,多得好幾年都學不完的教學,還有來天南地北的小夥伴。音樂的旅程即將開啟,你準備好瞭麼?</p>’;
$message .= ‘<p>您的賬號信息如下:</p>’;
$message .= ‘<p>’.sprintf(__(‘Username: %s’), $user_login) . ‘</p>’;
$message .= ‘<p>’.sprintf(__(‘Password: %s’), $plaintext_pass) . ‘</p>’;
$message .= ‘<p>請妥善保存好自己的賬戶信息,如果您忘記密碼,可以通過登錄窗口的密碼找回功能找回密碼。</p>’;
$message .= ‘<p>加入我們的微信,可及時獲取最新的教學內容!</p>’;
$message .= ‘<p><img src="http://cdn.daweijita.com/2014/07/dwjt-wx.jpg" /></p></div></body></html>’; @wp_mail($user_email, sprintf(__(‘[%s] Your username and password’), $blog_name), $message , $headers); }
//讓郵件支援html
add_filter( ‘wp_mail_content_type’, ‘wpdx_set_html_content_type’ );
function wpdx_set_html_content_type() {
return ‘text/html’;
}
}
1.新建一個名為 wpdx-new-user-notification 的PHP文件,復制上面的代碼到該文件中,根據自己的實際需求修改代碼內容,然後保存為 utf-8 無 BOM 編碼格式。
2.新建一個名為 new-user-notification 的文件夾,將上面的 wpdx-new-user-notification.php 添加到裡面,然後連同文件夾一起上傳到 /wp-content/plugins/ 目錄下
3.返回WP後臺,已安裝的外掛中啟用即可。
示例中的用戶歡迎郵件的效果如下:
參考資料:http://codex.wordpress.org/Function_Reference/wp_new_user_notification