admin管理员组

文章数量:1125590

I've been trying to edit the automatic email sent by WordPress to users when they register for my site. Currently, they register on a form made by the plugin called Forminator. However, the WordPress default email still sends which contains my admin url, and it's simply unappealing. I've resorted to using code since no other Plugin could stop/replace the default WP email without disabling emails entirely.

I tried the code below but it did not replace the default email:

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(__('Hey %s,'), $user->user_login) . "\r\n";
    $message .= sprintf(__('Thank you for registering with us! You are officially a Member who just earned the Loyalty Bagde, which grants you 50 Points!'), $plaintext_pass) . "\r\n";
    $message .= sprintf(__('Earn more rewards now by purchasing any products!'), $plaintext_pass) . "\r\n";
    $message .= wp_login_url() . "\r\n";

    wp_mail($user->user_email, sprintf(__('Your Brand New Account at %s'), $blogname), $message);

}
endif;

Is there something wrong with this code, or am I using the wrong hook? I have very little knowledge of coding so all help is appreciated.

I've been trying to edit the automatic email sent by WordPress to users when they register for my site. Currently, they register on a form made by the plugin called Forminator. However, the WordPress default email still sends which contains my admin url, and it's simply unappealing. I've resorted to using code since no other Plugin could stop/replace the default WP email without disabling emails entirely.

I tried the code below but it did not replace the default email:

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(__('Hey %s,'), $user->user_login) . "\r\n";
    $message .= sprintf(__('Thank you for registering with us! You are officially a Member who just earned the Loyalty Bagde, which grants you 50 Points!'), $plaintext_pass) . "\r\n";
    $message .= sprintf(__('Earn more rewards now by purchasing any products!'), $plaintext_pass) . "\r\n";
    $message .= wp_login_url() . "\r\n";

    wp_mail($user->user_email, sprintf(__('Your Brand New Account at %s'), $blogname), $message);

}
endif;

Is there something wrong with this code, or am I using the wrong hook? I have very little knowledge of coding so all help is appreciated.

Share Improve this question asked Dec 11, 2020 at 20:18 AnonymousAnonymous 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Try it with a filter:

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

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
   // filter stuff in $wp_new_user_notification_email here
   return $wp_new_user_notification_email;
}

Source: https://developer.wordpress.org/reference/functions/wp_new_user_notification/#comment-3130

small issue in your code related to message content formatted and also wp_new_user_notification_email hook allows you to customize the email sent to new users without modifying the core Wordpress files.

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

function custom_wp_new_user_notification_email($wp_new_user_notification_email, $user, $blogname) {
    // Customize the subject of the email
    $wp_new_user_notification_email['subject'] = sprintf(__('Welcome to %s!'), $blogname);

    // Customize the message body of the email
    $message  = sprintf(__('Hey %s,'), $user->user_login) . "\r\n";
    $message .= __('Thank you for registering with us!') . "\r\n";
    $message .= __('You are officially a Member who just earned the Loyalty Badge, which grants you 50 Points!') . "\r\n";
    $message .= __('Earn more rewards now by purchasing any products!') . "\r\n";
    $message .= wp_login_url() . "\r\n";

    $wp_new_user_notification_email['message'] = $message;

    // Return the modified email
    return $wp_new_user_notification_email;
}

本文标签: pluginsChanging the Default New User Notification Email