Tuesday, October 26, 2021
Affiliate Marketing Updates


WordPress Tutorials Archives – WP Engineer

If a new user registers at a WordPress site the new user and the administrator receive notification mails: User: From:…

By Staff , in Wordpress , at June 17, 2021


If a new user registers at a WordPress site the new user and the administrator receive notification mails:

User:

From: myBlog ([email protected])
Subject: [myBlog] Your username and password info

Username: new_user

To set your password, visit the following address:<http://myblog/wp-login.php?action=rp&key=3oCJkevP1ZSSb0P8DlOW&login=new_user>

http://myblog/wp-login.php

Admin:

From: myBlog ([email protected])
Subject: [myBlog] New User Registration

New user registration on your site myBlog:

Username: new_user

Email: [email protected]

Until version 4.8 the content of the mail was hard coded.The only way to alter the mails was to hook into wp_mail() or even phpmailer.

WordPress v4.9 now offers the ability to easily customize these mails by using the following filters:

The filters fire after the user has been added to the database.

The filters for the user’s and the admin’s email follow the same logic, just the filter and the variable names differ:

$wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname )

The parameters contain the following values:

  • $wp_new_user_notification_email_admin: Associative array with the keys to, subject, message, headers.
  • $user: A WP_User object of the registered user.
  • $blogname: A string containing the name of the blog the user registered to.

With these values at hand it’s easy to create your customized mail:

add_action( 'login_init', 'my_wp_new_user_notification_email_admin_init' );

function my_wp_new_user_notification_email_admin_init() {
    add_filter( 'wp_new_user_notification_email_admin', 'my_wp_new_user_notification_email_admin', 10, 3 );
}

function my_wp_new_user_notification_email_admin( $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;

}

(As always for filters: do not forget to return the altered variable.)  The mail now looks like this:

From: myBlog ([email protected])
Subject: [myBlog] New user new_user registered.

new_user ([email protected]) has registerd to your blog myBlog.

Of course, you can insert more data than provided by the filter. E.g., you can tell the admin, how many registered users the blog already has:

function my_wp_new_user_notification_email_admin($wp_new_user_notification_email, $user, $blogname) {

    $user_count = count_users();

    $wp_new_user_notification_email['subject'] = sprintf('[%s] New user %s registered.',
$blogname, $user->user_login);
    $wp_new_user_notification_email['message'] =
    sprintf( "%s has registerd to your blog %s.", $user->user_login, $blogname) .
"nnr" . sprintf("This is your %d. user!", $user_count['total_users']);

    return $wp_new_user_notification_email;

}

The parameter headers can be used to easily change some header information:

$wp_new_user_notification_email['headers'] = "From: myBlog <[email protected]> nr cc: Admin 2 <[email protected]>";

In this example the From information of the mail is changed and the user [email protected] is added as an additional recipient. As you can see, multiple headers entries are separated by “nr” (return and linefeed).

As said before this all could already be done by using wp_mail() and phpmailer but the new filters are way more convenient.



Source link

Comments