Customizing the User Registration Notification eMails

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

User:

From: myBlog (info@myBlog.com)
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 (info@myBlog.com)
Subject: [myBlog] New User Registration

New user registration on your site myBlog:

Username: new_user

Email: new_user@myblog.com

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 (MyBlog@myblog.com)
Subject: [myBlog] New user new_user registered.

new_user (new_user@user.com) 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) .
"\n\n\r" . 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 <myblog@myblog.com> \n\r cc: Admin 2 <admin2@mblog.com>";

In this example the From information of the mail is changed and the user admin2@myblog is added as an additional recipient. As you can see, multiple headers entries are separated by "\n\r" (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.

Create your own bulk actions

2016-10-21_13-46-04
Including version 4.6 it was quite difficult to add custom bulk actions to the WordPress admin pages. In version 4.7 a hook is added that simplifies the task a lot:

add_action('bulk_actions-{screen_id}', 'my_bulk_action');

Defining the hook

As an example we'll use the post page, the variables are named accordingly.

add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );

Continue reading …

Test or Meet at WordCamp San Francisco and Win a Plugin License!

WordCamp San Francisco

Next week I will be at WordCamp San Francisco and a week later at the WooConf! Maybe one or antoher of our loyal readers will also be there. It would be great to meet you in person!

Frank and I had such a blast at WordCamp Europe in Sofia last month. It was great to meet many of our old WordPress buddies and get to know even more WodPress freaks there.

We also had the chance to show many people the newest version of our Multlingual WordPress plugin MultlingualPress. Everybody was very excited about it. Because it is a clean solution without any performance issues. It is very userfriendly, build on existing core functionality, no compromises in SEO, etc. ... see all features here.

Many people we talked to, had the problem, that they are tight to their current multilanguage solution. If they deactivate their plugin, the content is gone. Big issue if you want to move to a better performing solution. Very soon, it won't be anymore, espacially with your help testing it. We are currently building a WPML importer to easily switch to MultlingualPress. 🙂 Other importer will follow soon too.

There are two ways to win a pro plugin of MultilingualPress

We are looking for some engaging tester for our new WPML importer. If you participate, you can get our pro version for free. If you are interested please contact Frank directly at frank@bueltge.de, for more information read our post on Google+. We really appreciate your support!

On #WeSpeakWP we will have a little contest going for WordCamp San Francisco. Get a sticker in your language from me, if available, at WordCamp San Franciso or WooConf and post a photo/selfie with the sticker on Twitter. Not to forget with hashtag #WeSpeakWP ! The more fun and crazy the photos are, even better! 🙂

WordCamp San Francisco
WordCamp San Francisco

Of course there is also a free version available, so try it out. Here you can see the differences between the pro and free version. Not to forget, with the pro version you get an excellent support!

I'm really looking forward to meet you in San Francisco! Let me in our comments know if you will be there! Can't wait!

Inform user about automatic comment closing time

To prevent spammers from flooding old articles with useless comments you can set WordPress to close comments after a certain number of days:
close-comments
It might be surprising for some users if the comments are closed automatically so it might be a good idea to inform them about the remaining time.

add_action( 'comment_form_top', 'topic_closes_in' );

function topic_closes_in() {
    global $post;
    if ($post->comment_status == 'open') {
        $close_comments_days_old = get_option( 'close_comments_days_old' );
        $expires = strtotime( "{$post->post_date_gmt} GMT" ) +  $close_comments_days_old * DAY_IN_SECONDS;
        printf( __( '(This topic will automatically close in %s. )', 'domain' ),  human_time_diff( $expires ));
    }
}

While the code should be almost self explanatory there is an interesting function not every WordPress developer might know: human_time_diff(). This function is hidden in the .../wp-includes/formatting.php file. It is originally planned to be used in themes to provide a more "human" touch to the date/time a post was written. Since the function does not care if the date is in the past or in the future we can use it for our needs.

close comment example