Forked from strangerstudios/pmpro-change-role-custom-roles.php
Last active
May 20, 2019 18:44
-
-
Save LMNTL/1d95acff3c63aff7c0de916b09117574 to your computer and use it in GitHub Desktop.
Add a custom role for new PMPro members at checkout. Add this code to your active theme's functions.php or a custom plugin. - based on https://gist.github.com/strangerstudios/7280471
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 a 'myrole' custom role created. | |
Members signing up get the 'myrole' role in addition to any other roles they already have. | |
Members cancelling are given the subscriber role. | |
Admin users are ignored. | |
Usable with or without the PMPro Roles Add On. | |
*/ | |
function my_pmpro_after_change_membership_level($level_id, $user_id) | |
{ | |
/*--- Change this line to change the name of the role ---*/ | |
$role_to_add = 'myrole'; | |
//get user object | |
$wp_user_object = new WP_User($user_id); | |
//ignore admins | |
if(in_array("administrator", $wp_user_object->roles)) | |
return; | |
if( !empty( $level_id ) ) | |
{ | |
//do they already have the role? | |
if( !in_array( $role_to_add, $wp_user_object->roles ) ){ | |
//if not, add it to their existing roles | |
$wp_user_object->add_role( $role_to_add ); | |
} | |
} | |
else | |
{ | |
//Cancelling. Give them Subscriber role. | |
$wp_user_object->set_role('subscriber'); | |
} | |
} | |
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 20, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment