wpsharks / s2member-kb

The s2Member® and s2Member® Pro Knowledge Base
9 stars 4 forks source link

Sending Emails on Membership Changes #310

Open KTS915 opened 8 years ago

KTS915 commented 8 years ago

s2Member already provides the ability to send emails to users both when they first register and if they later modify their membership by using a Billing Modification form or button. It also enables you to send emails to users before their membership is due to expire.

But there may be other occasions for which you would like to be able to automate the sending of emails. For example, you may wish to have emails automatically sent out to users who change their membership level by using a form provided by another plugin, or where you change a user’s membership manually on their WordPress admin page.

The plugin, Better Notifications for WordPress, does enable emails to be sent out whenever a user changes their membership level, irrespective of how that change was made. But that has the limitation that the same email content will be sent no matter what change is made. So a change from subscriber to level 1 will cause the same email to be sent as a change from level 2 to level 3. You might prefer to send out emails that contain content specific to the type of change made.

For example, many site owners do not allow their users at “subscriber” level to access protected content. Instead, they use this level as a means of allowing them to manually approve (or reject) membership applications. Approval means manually upgrading the user to a Level 1 member. So it would be nice to have a way to send out an email designed specifically for this purpose.

You might also want to send emails whenever any other role change is made, in which case you will need a different email for someone who downgrades to Level 1 membership, because they won’t just have been approved.

You might also want to send an email to confirm that a user’s account has been deleted.

The example below shows how to do all these things. You can simply omit the functions you don’t want.

Start by creating a file called email-on-role-change.php Then add the code below, customizing the subject lines and messages as you like.

<?php
/* SEND EMAIL WHEN MEMBERSHIP LEVEL CHANGES TO LEVEL 1 */
function user_role_approve_email( $user_id, $old_user_data ) {
    $site_url = site_url();

    // get old user info
    $old_roles = $old_user_data->roles;

    // get new user info
    $user_info = get_userdata( $user_id );
    $new_roles = $user_info->roles;

    // send email when membership is first approved, i.e. from subscriber to s2member_level1
    if( in_array( 'subscriber', $old_roles ) && in_array( 's2member_level1', $new_roles ) ) {
        $to = $user_info->user_email;
        $subject = 'Welcome to the ' . get_bloginfo ( 'name' ) . ' website!';
        $message = 'Hello, ' . $user_info->display_name . '!' "\r\n\r\n";
        $message = 'Your membership registration has now been approved on the ' .$site_url. ' website.' "\r\n";
        wp_mail( $to, $subject, $message );
    }

    // send email when membership changes to Level 1 from any higher role
    else if( !in_array( 'subscriber', $old_roles ) && in_array( 's2member_level1', $new_roles ) ) {
        $to = $user_info->user_email;
        $subject = 'Your membership status has changed';
        $message = 'Hello, ' . $user_info->display_name . '!' "\r\n\r\n";
        $message = 'Your membership status has changed on the ' . get_bloginfo ( 'name' ) . ' website. You are now a Level 1 Member.' "\r\n";
        wp_mail( $to, $subject, $message );
    }
}
add_action( 'profile_update', 'user_role_approve_email', 10, 2 );

/* SEND EMAIL WHEN OTHER MEMBERSHIP LEVELS CHANGE */
function user_role_update_email( $user_id, $new_role, $old_role ) {
    $site_url = site_url();
    $user_info = get_userdata( $user_id );

    if ( !empty( $old_role ) && $new_role == 's2member_level2' ) {
        $to = $user_info->user_email;
        $subject = "Your membership status has changed";
        $message = 'Hello, ' . $user_info->display_name . '!' "\r\n\r\n";
        $message = 'Your membership status has changed on the ' . get_bloginfo ( 'name' ) . ' website. You are now a Level 2 Member.' "\r\n";
        wp_mail( $to, $subject, $message );
    }
    else if ( !empty( $old_role ) && $new_role == 's2member_level3' ) {
        $to = $user_info->user_email;
        $subject = "Your membership status has changed";
        $message = 'Hello, ' . $user_info->display_name . '!' "\r\n\r\n";
        $message = 'Your membership status has changed on the ' . get_bloginfo ( 'name' ) . ' website. You are now a Level 3 Member.' "\r\n";
        wp_mail( $to, $subject, $message );
    }
    else if ( !empty( $old_role ) && $new_role == 's2member_level4' ) {
        $to = $user_info->user_email;
        $subject = "Your membership status has changed";
        $message = 'Hello, ' . $user_info->display_name . '!' "\r\n\r\n";
        $message = 'Your membership status has changed on the ' . get_bloginfo ( 'name' ) . ' website. You are now a Level 4 Member.' "\r\n";
        wp_mail( $to, $subject, $message );
    }
}
add_action( 'set_user_role', 'user_role_update_email', 10, 3);

/* SEND EMAIL WHEN MEMBERSHIP IS DELETED */
function delete_user_email( $user_id ) {
    global $wpdb;
    $user_obj = get_userdata( $user_id );
    $email = $user_obj->user_email;
    $site_url = site_url();
    $to = $email;
    $subject = "Your account has been deleted";
    $message = 'Hello, ' . $user_info->display_name . '!' "\r\n\r\n";
    $message = 'Your membership on the ' . get_bloginfo ( 'name' ) . ' website has now been deleted. We are sorry to see you go.' "\r\n";
    wp_mail( $to, $subject, $message );
}
add_action( 'delete_user', 'delete_user_email' );

/* FILTERS TO SET EMAIL FROM NAME AND VALUE */
function my_wp_mail_from_name( $name ) {
    return 'My Website Name';
}
add_filter( 'wp_mail_from_name', 'my_wp_mail_from_name' );

function my_wp_mail_from( $content_type ) {
    return 'me@email.com';
}
add_filter( 'wp_mail_from', 'my_wp_mail_from' );

Then upload this file to your site's mu-plugins folder.

patdumond commented 8 years ago

@KTS915: Yay! Thanks. I've had this (or something like it) on my list for ages.

KTS915 commented 8 years ago

@patdumond: Thanks! This took me some time to figure out. The WP Codex on this is horrible.