Last active
April 17, 2021 00:18
-
-
Save strangerstudios/7280471 to your computer and use it in GitHub Desktop.
Change roles for new PMPro members to custom roles at checkout based on level. Add this code to your active theme's functions.php or a custom plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
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 == 1) | |
{ | |
//New member of level #1. Give them Bronze Member role. | |
$wp_user_object->set_role('bronzemember'); | |
} | |
elseif($level_id == 2) | |
{ | |
//New member of level #2. Give them Silver Member role. | |
$wp_user_object->set_role('silvermember'); | |
} | |
elseif($level_id == 3) | |
{ | |
//New member of level #3. Give them Gold Member role. | |
$wp_user_object->set_role('goldmember'); | |
} | |
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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Give New Members Author Role" at Paid Memberships Pro here: https://www.paidmembershipspro.com/give-new-members-author-role/