我正在尝试使用wp_new_user_notification
自定义默认的wordpress用户通知,我已将其包含在mu-plugins文件夹中,以便获得优先权。
<?php
// 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 your blog %s:'), get_option('blogname')) . "rnrn";
$message .= sprintf(__('Username: %s'), $user_login) . "rnrn";
$message .= sprintf(__('E-mail: %s'), $user_email) . "rn";
@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
if ( empty($plaintext_pass) )
return;
$message = __('Hi there,') . "rnrn";
$message .= sprintf(__("Welcome to %s! Here's how to log in:"), get_option('blogname')) . "rnrn";
$message .= wp_login_url() . "rn";
$message .= sprintf(__('Username: %s'), $user_login) . "rn";
$message .= sprintf(__('Password: %s'), $plaintext_pass) . "rnrn";
$message .= sprintf(__('If you have any problems, please contact me at %s.'), get_option('admin_email')) . "rnrn";
$message .= __('Adios!');
wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
}
}
?>
但是,当我使用我的电子邮件地址创建测试用户时,它会向我发送此模板
Username: test
To set your password, visit the following address:
example.com
我希望能够向用户发送密码并将链接更改为登录。有人可以帮忙吗?
答案 0 :(得分:2)
在WP 4.9中,wp_new_user_notification()函数已被更改,我们有一个新的wp_new_user_notification_email()过滤器可供使用。我有以下代码工作,密码除外。传递了一个值,但是当我尝试登录时,密码被拒绝。
add_filter( 'wp_new_user_notification_email' , 'edit_user_notification_email', 10, 3 );
function edit_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
$message = sprintf(__( "Welcome to %s! Here's how to log in:" ), $blogname ) . "\r\n";
$message .= wp_login_url() . "\r\n";
$message .= sprintf(__( 'Username: %s' ), $user->user_login ) . "\r\n";
$message .= sprintf(__( 'Password: %s' ), $user->user_pass ) . "\r\n\r\n";
$message .= sprintf(__( 'If you have any problems, please contact me at %s.'), get_option( 'admin_email' ) ) . "\r\n";
$message .= __('Adios!');
$wp_new_user_notification_email['message'] = $message;
return $wp_new_user_notification_email;
}
https://developer.wordpress.org/reference/functions/wp_new_user_notification/
答案 1 :(得分:1)
感谢此解决方案,它可以工作,但是内部存在一些错误。 此函数返回md5编码的密码,而不是新用户的纯文本密码。 我认为解决此问题的最佳方法是在函数中设置新密码(wp_set_password):
function my_wp_new_user_notification_email($wp_new_user_notification_email,$user,$blogname) {
$newpass = wp_generate_password( 10, true, false );
wp_set_password($newpass,$user->ID);
$message = sprintf(__( "Welcome to %s! Here's how to log in:" ), $blogname ) . "\r\n";
$message .= wp_login_url() . "\r\n";
$message .= sprintf(__( 'Username: %s' ), $user->user_login ) . "\r\n";
$message .= sprintf(__( 'Password: %s' ), $newpass) . "\r\n\r\n";
$message .= sprintf(__( 'If you have any problems, please contact me at %s.'), get_option( 'admin_email' ) ) . "\r\n";
$message .= __('Adios companieros!');
$wp_new_user_notification_email['message'] = $message;
return $wp_new_user_notification_email;
}