如何更改新用户的电子邮件模板


10

当我添加新客户时,电子邮件将以以下格式发送给新用户:

From: WordPress [wordpress@siteurl.com]
Subject: [site name] Your user name and password
Message:
         username: user
         Password: password
         siteurl.com/wp-login.php

现在,我想像这样更改这种格式:

From: My Site Name [info@siteurl.com]
Subject: siteurl.com customer account activated
Message:
       Your customer account has been activated.

       Your Login credentials are:

       Username: user email
       Password: password

       Thanks,
       abcd

我尝试了这个答案,但是没有用。

我怎样才能做到这一点?

Answers:


17

对于2018年及以后的用户:

大卫·加德(David Gard)的答案仍然有效,但是它已经过时了,并且有一种新的更好/更干净的方法来实现(不再需要插件)。

自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;
}

7

新的用户通知电子邮件由函数创建并发送,该函数wp_new_user_notification()位于wp-includes / plugable.php中

此功能中没有过滤器挂钩,可让您操纵电子邮件的输出,但是您当然可以通过插件覆盖任何可插拔功能。

注意 -您只能从插件内部而不是主题内部覆盖可插入函数。

有关可插拔功能的详细信息以及可用功能的完整列表,请参见此处-http: //codex.wordpress.org/Pluggable_Functions

这段代码将创建该插件,而不是wp-includes / plugable.php中使用的插件(将其保存在wp-content / plugins /中自己的文件中)。

我尚未为您定制它,但这应该可以助您一臂之力。

<?php
/**
 * Plugin Name: Custom new user notification email
 * Description: Overwrites the pluggable 'wp_new_user_notification()' plugin to allow the sending of a custom email
 * Author: David Gard
 * Version: 1.0
 */

if ( !function_exists('wp_new_user_notification') ) :
/**
 * Pluggable - Email login credentials to a newly-registered user
 *
 * A new user registration notification is also sent to admin email.
 *
 * @since 2.0.0
 *
 * @param int    $user_id        User ID.
 * @param string $plaintext_pass Optional. The user's plaintext password. Default empty.
 */
function wp_new_user_notification($user_id, $plaintext_pass = ''){

    $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  = 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(__('E-mail: %s'), $user->user_email) . "\r\n";

    @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

    if ( empty($plaintext_pass) )
        return;

    $message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
    $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
    $message .= wp_login_url() . "\r\n";

    wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

}
endif;

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.