WordPress Registration Email by Role

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 '[email protected]';
}


// 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.

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino