Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LMNTL/1d95acff3c63aff7c0de916b09117574 to your computer and use it in GitHub Desktop.
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
<?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