WordPress 对新用户注册有一个欢迎邮件,一般告知用户的用户名和密码,以及登录的地址;还有一封发给管理员的通知邮件,告诉管理员新用户的用户名和邮箱。这两封邮件的内容一般都比较简单,今天我们就一起来学习一下自定义这两个邮件。

首先,我们需要了解一下 WordPress 用来发送这两个邮件的函数:


<?php wp_new_user_notification( $user_id, $plaintext_pass ) ?>

默认的邮件代码如下:


// 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
		);
	}
}