latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Send email or two when role changes manually

Home Forums Community Forum Send email or two when role changes manually

This topic contains 15 replies, has 3 voices. Last updated by  Loretta 3 years, 10 months ago.

Topic Author Topic
Posted: Sunday Jan 20th, 2013 at 8:16 pm #38532
Loretta
Username: companions

I have 2 levels on membership on the site I am working on. For now its a free site but the members have to be approved before they can access the Main Level of the site.

We have them sign up for a free membership, and then when their “Application” is approved we manually change the role to the Main Level or Approved Member Level of the site.

When we upgrade them, we would like to have an automated email sent out to the member, ideally would would like to have two emails sent on upgrade, or have the ability to attach a file to the email that goes out.

I searched around and found a topic that suggested that this be added to the functions.php of my theme.
———————————————————————-

function user_role_update( $user_id, $new_role ) {
        $site_url = get_bloginfo('wpurl');
        $user_info = get_userdata( $user_id );
        $to = $user_info->user_email;
        $subject = "Role changed: ".$site_url."";
        $message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
        wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'user_role_update', 10, 2);

—————————————————————————-
Which sounds and looks good but where exactly in the functions.php would this be best placed?, does it matter?

and can use html formatted code in the email or have this come up as an option for editing like the new member email?

thank you in advance for the help.

Loretta

  • This topic was modified 3 years, 11 months ago by  Bruce. Reason: Code formatted for better readability

List Of Topic Replies

Viewing 15 replies - 1 through 15 (of 15 total)
Author Replies
Author Replies
Posted: Tuesday Jan 22nd, 2013 at 9:40 pm #39106
Bruce
Username: Bruce
Staff Member

Thanks for your inquiry.

I searched around and found a topic that suggested that this be added to the functions.php of my theme.

The code that you posted looks correct, however I would suggest not putting this code into your theme’s functions.php file. This is because themes do update at times (though much less often than most plugins). If your theme did update, your code would be lost.

Instead, I would recommend putting this code into a Must-Use Plugin. These plugins are automatically loaded into your site, which is what you want, and will not be destroyed by updates.

To create a Must-Use Plugin, put the code posted into a .php file, by saving it with your favorite text editor. Note that .php files start with <?php&#91;/hilite&#93;. Therefore, the code that you would put into your new file would look like this:

&#91;hilite pre_code&#93;
<?php
function user_role_update( $user_id, $new_role ) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Role changed: ".$site_url."";
$message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'user_role_update', 10, 2);

Once the file is created, you upload this file via FTP to your server, to your /wp-content/mu-plugins/ directory. If you do not have this directory, please create it.

Please let us know if problems persist.

Posted: Wednesday Jan 23rd, 2013 at 4:28 pm #39235
Loretta
Username: companions

I tired your idea, file pasted below I called it role_change.php, but as soon as put this on the server, in the mu-plugins directory, and I tried to access the members list to test it out I got this error message:

Parse error: syntax error, unexpected ‘&’ in /home/jmessage/public_html/cw/wp-content/mu-plugins/role_change.php on line 5

Was there something I needed to add or change?

<?php
function user_role_update( $user_id, $new_role ) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Role changed: ".$site_url."";
$message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'user_role_update', 10, 2);

thanks;

Loretta

Posted: Friday Jan 25th, 2013 at 1:15 pm #39521
Bruce
Username: Bruce
Staff Member

Thanks for the follow-up.

If you’ll check your original code sample, you had HTML entities in place for the greater-than (>) symbol. This is likely what was causing the issue. I updated my code sample in my previous post, please try that instead.

Posted: Friday Jan 25th, 2013 at 1:34 pm #39527
Loretta
Username: companions

Thanks I will… I had copied that code, and wasn’t sure what it should say.. I will be testing it out later today.

appreciate your help very much.

Posted: Monday Jan 28th, 2013 at 4:13 pm #39798
Loretta
Username: companions

Yay! That worked… so my next question is, can I enter formatted text where is says congratulations etc?

I need to include a couple of paragraphs of information in the email they receive once they are approved.

thanks again!

Loretta

Posted: Monday Jan 28th, 2013 at 5:23 pm #39809
Loretta
Username: companions

Problem, this is being sent to new users too, not just upgrade users how do I stop that from happening? I only need it to go to members who get upgraded.. not on every role change.

thanks

Posted: Wednesday Jan 30th, 2013 at 11:15 am #40003
Loretta
Username: companions

I REALLY NEED SOMEONE TO GET BACK TO ME ON THIS!!! TODAY I AM GOING LIVE WITH THIS SITE AND BEEN WAITING FOR 3 days for you to get back to me.

When a new member joins they are getting the roe change email too… how do I make it show up ONLY when someones role is changed NOT when they sign UP

Also when it send the Role information it is showing up ad the S2member label NOT the label that I chose I need to know how to make it use MY lables for membership roles, instead of S2members names for the roles.

For instance, I have Approved Member, and S2 calls this s2member_level1

I have checked the setting to Force WordPress® to use your Labels? but in the email it says s2member_level1

in addition, I would like to add an extra line of text after is says: Your role has changed on http://thesiteaddress.com, congratulations you are now an s2member_level1

  • This reply was modified 3 years, 11 months ago by  Loretta.
  • This reply was modified 3 years, 11 months ago by  Loretta.
  • This reply was modified 3 years, 11 months ago by  Loretta.
Posted: Saturday Feb 2nd, 2013 at 4:53 am #40342
Staff Member
That’s about as far as we can go on custom coding. We’ll be happy to provide you with more leads on where to find certain things, or to share ideas with you, but we can’t debug any custom code that you end up with; due to time constraints :-) Please see: s2Member® » Support Policy » Within Scope for some clarification on this. Thanks!
Posted: Monday Feb 4th, 2013 at 12:53 am #40582
Loretta
Username: companions

So I get this code from your forum where you helped someone else, but you won’t help me?

I just wanted it to do the same thing they did, I just need an email that goes out that tells them they have been upgraded. I don’t want the same emails to go to people that are just signing up.

I would pay for your time if there was that option. I really need this to be able to work.

Posted: Tuesday Feb 5th, 2013 at 12:07 pm #40862
Loretta
Username: companions

If I were to OMIT the part about set_user_role IN this line of code, would that stop it from sending when the new members sign up and only send it when the role is updated?

}add_action( 'set_user_role', 'user_role_update', 10, 2);
Posted: Wednesday Feb 6th, 2013 at 5:39 pm #40934
Bruce
Username: Bruce
Staff Member

If I were to OMIT the part about set_user_role IN this line of code, would that stop it from sending when the new members sign up and only send it when the role is updated?

No, this would not achieve what you’re looking to do. I took a quick look at the processes within /wp-includes/capabilities.php, where this hook is established. It looks like the functionality that you’re looking to set up here would not be possible with this hook. WordPress does not pass the previous role that the User had, so there’s no way to tell if the User was upgraded or not.

Instead of using this hook, I would recommend using s2Member’s API Notifications (specifically Modification Notifications). s2Member only sends Modification Notifications when a User was previously signed up, so this should work perfectly for you.

See: Dashboard -› s2Member® -› API / Notifications -› Modification Notifications
See Also: Knowledge Base » Building An API Notification Handler

Posted: Wednesday Feb 6th, 2013 at 10:24 pm #40959
Loretta
Username: companions

Great Thank you So Much I will take a look.

Posted: Wednesday Feb 13th, 2013 at 3:41 pm #41622
Loretta
Username: companions

I checked the Modification API and it says : “This Notification will NOT be processed for Free Subscribers that register without going through your Payment Gateway at all. This Notification will NOT be processed for “new” Users/Members ( see: Signup Notifications for that scenario ). And, this Notification will NOT be processed for Independent Custom Capability purchases. ”

The members on the site I am working on do not pay for their membership.

They sign up for the free membership, and then we approve them or upgrade to the Full Membership, but we are not collecting any money.

According to the info, this only works if they go through the payment gateway.

Did I miss something?

thanks

Posted: Friday Feb 15th, 2013 at 3:43 am #41800
Staff Member

Thanks for the heads up on this thread :-)

Here is an example showing how you might track User Role changes in WordPress® with more flexibility.

Please create this directory and file:
/wp-content/mu-plugins/s2-hacks.php
(NOTE: these are MUST USE plugins, see: http://codex.wordpress.org/Must_Use_Plugins)
(See also: http://www.s2member.com/kb/hacking-s2member/)

<?php
add_action('init', 's2_role_change_event_handler::init', 1);

class s2_role_change_event_handler
	{
		public static $current_user_id;
		public static $current_user_old_role;
	
		public static function init()
			{
				$current_user = wp_get_current_user();
				
				static::$current_user_id = $current_user->ID;
				static::$current_user_old_role = reset($current_user->roles);
				
				add_action('set_user_role', 's2_role_change_event_handler::event', 10, 2);
			}
		
		public static function event($user_id, $new_role)
			{
				if($user_id === static::$current_user_id && $new_role !== static::$current_user_old_role)
					{
						// Handle this role change event. The role for this User is changing.
						// Here you might have custom code that does something more.
					}
			}
	}
Posted: Friday Feb 15th, 2013 at 10:57 am #41885
Loretta
Username: companions

Great I will give that a try, thanks!

Loretta

Viewing 15 replies - 1 through 15 (of 15 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.