0

I'm wanting to send out a custom html email to a new user based on their role during registration. So, I have 2 roles that will be specifically registering on the site: photographer and subscriber and I want one email welcoming photographers to the site and another email sent when subscribers register.

I know this is a pluggable function and you have to override it in a plugin - started with this post but it doesn't address different emails by roles. Not sure if I can create an if statement that says if current user is role because I'm not sure if current user is coming into factor when the suer is being created, if that makes sense. Any direction on this would be greatly appreciated, thank you!

0

Scroll down a little bit, most of it is WordPress default code but you must add everything to your plugin or you might break it! I've also added few useful snippets below. Copy-paste them to same plugin.

<?php

/*
 * Plugin Name: Emails
 * Plugin URI: http://www.your-site.com
 * Description: Different registration email for different roles
 * Author: Your name
 * Author URI: http://www.your-site.com
*/ 

// Registration email
if( ! function_exists( 'wp_new_user_notification' ) ) {

    function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {

        if ( $deprecated !== null ) {

            _deprecated_argument( __FUNCTION__, '4.3.1' );
        }

        global $wpdb, $wp_hasher;
        $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 that gets sent to admin if new user registers
        $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(__('Email: %s'), $user->user_email) . "\r\n";

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

        // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notifcation.
        if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
            return;
        }

        // Generate something random for a password reset key.
        $key = wp_generate_password( 20, false );

        /** This action is documented in wp-login.php */
        do_action( 'retrieve_password_key', $user->user_login, $key );

        // Now insert the key, hashed, into the DB.
        if ( empty( $wp_hasher ) ) {
            require_once ABSPATH . WPINC . '/class-phpass.php';
            $wp_hasher = new PasswordHash( 8, true );
        }
        $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
        $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );



        ///////////////////////////////////////////////////////////////////
        //  Your code starts here, rest of it is WordPress default code  //
        ///////////////////////////////////////////////////////////////////

        // First role
        if( $user->roles == 'some_vip_role' ) {

            $email_subject = 'Welcome VIP!';

            $message = 'message content for VIP';
        } 
        // Second role
        else if( $user->roles == 'some_other_role' ) {

            $email_subject = 'Welcome other role!';

            $message = 'message content for other role';
        }

        // And so on.. 


        // Send email
        wp_mail( $user->user_email, $email_subject, $message );


        ////////////////////////////////////////////////////////////
        //  Make sure to add this link to each $message           //
        //  This is where user can set his/her first password     //
        ////////////////////////////////////////////////////////////
        $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n";

    }   
}

  • Play around with roles and make sure that everything works properly.
  • Also add else statement to the end and use a generic $email_subject and $message in case you'll add a new role in future and forget to change the plugin.
  • List of properties that $user object has can be found here.
  • Don't forget to add a link where user has to go to set a password!
  • Again: test, test and test - make several dummy users with different roles to test it out.

Few other useful snippets - add them before main function, just in case:

// Change "from" email
add_filter( 'wp_mail_from', 'new_mail_from_name' );

function new_mail_from_name() {

    return 'hello@you-site.com';
}


// Change "from" name
add_filter( 'wp_mail_from_name', 'new_mail_from' );

function new_mail_from() {

    return 'Your site';
}

Happy coding and let me know how it worked out for you.

  • Thank you for the extensive response to this request, appreciate it very much! Unfortunately when I plugged it in, it didn't send any email. Rather than bugging you and going back and forth to debug I found a solution. Come to find there were two different systems at work when registering. Registering the photographer role was done through wordrpess' account registration and the subscriber role creation was handled by woocommerce. This helped out since all I had to do was change the generic response for the wordpress registration & then change the account registration template for woocommerce. – jameslcarton May 7 '16 at 22:51
  • That's great that you found a solution. Only reason this could not work is that your own roles on registration were set after this code runs. I tried it and it worked with default user roles. I would still accept this answer because you didn't mention WooCommerce (which is off-topic anyway) and this is the answer to "different emails to different roles in WordPress core". – N00b May 7 '16 at 23:01
  • Yes, didn't realize the process for the other role I was trying to register until I got in there and tried to use the code you provided. I'll tinker around with it some more as I'm sure the code you provided works! Thanks again! – jameslcarton May 8 '16 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.