Sunday, August 9, 2020

How to add custom role in paid memberhsip pro specific level

1) have to install https://www.paidmembershipspro.com/downloads/pmpro-customizations.zip
2) add that code 
/*
	This code assumes you already have custom roles created
	for bronzemember, silvermember, and goldmember.
	Members signing up for membership level #1 get "Bronze Member" role.
	Members signing up for membership level #2 get "Silver Member" role.
	Members signing up for membership level #3 get "Gold Member" role.
	Members cancelling are given the subscriber role.
	Admin users are ignored.
*/
function my_pmpro_after_change_membership_level($level_id, $user_id)
{
	//get user object
	$wp_user_object = new WP_User($user_id);
  
	//ignore admins
	if(in_array("administrator", $wp_user_object->roles))
		return;

	if($level_id == 2)
	{
		//New member of level #2. Give them Silver Member role.
		$wp_user_object->set_role('stm_lms_instructor');
	}
	elseif($level_id == 0)
	{
		//Cancelling. Give them Subscriber role.
		$wp_user_object->set_role('subscriber');
	}
}
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);

That code has to add in pmpro-change-role-custom-roles.php
When you will add that code you have to know which level ID will get which role user.
To pick up your website all roles without admin install user role editor plugin and then you have to go to users > users role editor and the select role and know what is role name and ID. 
That role ID will go in that code ID parameter. 
In my case, that role ID was: stm_lms_instructor. 

Let me know if anything else. 
Thank you.

0 comments:

Post a Comment