向下滚动一点,大部分是WordPress的默认代码,但您必须将所有内容添加到插件中,否则可能会破坏它!我还在下面添加了一些有用的片段。复制粘贴到同一个插件。
<?php
/*
* Plugin Name: Emails
* Plugin URI: http://www.your-site.com
* Description: Different registration email for different roles
* Author: Your name
* Author URI: http://www.your-site.com
*/
// Registration email
if( ! function_exists( \'wp_new_user_notification\' ) ) {
function wp_new_user_notification( $user_id, $deprecated = null, $notify = \'\' ) {
if ( $deprecated !== null ) {
_deprecated_argument( __FUNCTION__, \'4.3.1\' );
}
global $wpdb, $wp_hasher;
$user = get_userdata( $user_id );
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option(\'blogname\'), ENT_QUOTES);
// Message that gets sent to admin if new user registers
$message = sprintf(__(\'New user registration on your site %s:\'), $blogname) . "\\r\\n\\r\\n";
$message .= sprintf(__(\'Username: %s\'), $user->user_login) . "\\r\\n\\r\\n";
$message .= sprintf(__(\'Email: %s\'), $user->user_email) . "\\r\\n";
@wp_mail(get_option(\'admin_email\'), sprintf(__(\'[%s] New User Registration\'), $blogname), $message);
// `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn\'t sent a user notifcation.
if ( \'admin\' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
return;
}
// Generate something random for a password reset key.
$key = wp_generate_password( 20, false );
/** This action is documented in wp-login.php */
do_action( \'retrieve_password_key\', $user->user_login, $key );
// Now insert the key, hashed, into the DB.
if ( empty( $wp_hasher ) ) {
require_once ABSPATH . WPINC . \'/class-phpass.php\';
$wp_hasher = new PasswordHash( 8, true );
}
$hashed = time() . \':\' . $wp_hasher->HashPassword( $key );
$wpdb->update( $wpdb->users, array( \'user_activation_key\' => $hashed ), array( \'user_login\' => $user->user_login ) );
///////////////////////////////////////////////////////////////////
// Your code starts here, rest of it is WordPress default code //
///////////////////////////////////////////////////////////////////
// First role
if( $user->roles == \'some_vip_role\' ) {
$email_subject = \'Welcome VIP!\';
$message = \'message content for VIP\';
}
// Second role
else if( $user->roles == \'some_other_role\' ) {
$email_subject = \'Welcome other role!\';
$message = \'message content for other role\';
}
// And so on..
// Send email
wp_mail( $user->user_email, $email_subject, $message );
////////////////////////////////////////////////////////////
// Make sure to add this link to each $message //
// This is where user can set his/her first password //
////////////////////////////////////////////////////////////
$message .= \'<\' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), \'login\') . ">\\r\\n\\r\\n";
}
}
扮演各种角色,确保一切正常同时添加
else statement 最后使用泛型
$email_subject 和
$message 以防将来添加新角色时忘记更改插件属性列表
$user 可以找到对象
here.
Don\'t forget to add a link where user has to go to set a password! 再次:测试、测试和测试-让几个具有不同角色的虚拟用户进行测试
其他一些有用的代码片段-将它们添加到主函数之前,以防万一:
// Change "from" email
add_filter( \'wp_mail_from\', \'new_mail_from_name\' );
function new_mail_from_name() {
return \'hello@you-site.com\';
}
// Change "from" name
add_filter( \'wp_mail_from_name\', \'new_mail_from\' );
function new_mail_from() {
return \'Your site\';
}
祝您编码愉快,并告诉我您的工作结果。