如何更改默认注册电子邮件?(插件和/或非插件)


54

注册新用户后,WP会发送一封电子邮件,其中包含登录名/密码以及指向登录页面的链接。

有没有办法更改此默认电子邮件模板?我也想更改主题和发件人。

编辑:对于任何有兴趣的人,这里是一个插件解决方案。

Answers:


62

使用wp_new_user_notification()可插入功能发送新用户电子邮件,这意味着您可以覆盖它:

// 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')) . "\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);

    }
}

@Bainternet我似乎无法正常工作,我已将其添加到我的功能文件中,但是标准电子邮件一直在发送。我在多站点上,但这没关系吧?

6
现在就可以了,它似乎只能作为单独的插件工作,而不能将其添加到functions.php文件中。现在它可以完美地工作了,再次感谢您提供的出色代码!

它也适用于多站点吗?我可以看到多站点在ms-functions.php中具有一堆用于发送通知电子邮件的功能。
西西尔

wpmu_signup_user_notification我认为多站点用途。
Wyck

这个答案已经有好几年了。接受的答案对我不起作用。(将其添加到functions.php中与新用户注册时发送的任何电子邮件没有任何区别。)我是否应该发布一个新问题?
Kit Johnson

22

对于2018年及以后的用户:

自WordPress 4.9.0起,您可以使用新的过滤器(不再需要插件):

发送给Admin的电子邮件上的用法示例(您可以将其粘贴到主题的functions.php中):

add_filter( 'wp_new_user_notification_email_admin', 'custom_wp_new_user_notification_email', 10, 3 );

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    $wp_new_user_notification_email['subject'] = sprintf( '[%s] New user %s registered.', $blogname, $user->user_login );
    $wp_new_user_notification_email['message'] = sprintf( "%s ( %s ) has registerd to your blog %s.", $user->user_login, $user->user_email, $blogname );
    return $wp_new_user_notification_email;
}

或者,可以使用wp_new_user_notification_emailwp_new_user_notification_email_admin过滤器。有兴趣者可以检查出完整的文档和源代码wp_new_user_notification()
皮特

谢谢Pete,它看起来像是4.9.0中引入的,看起来是一个更好的解决方案。
Edu Wass

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.