Skip to content

Instantly share code, notes, and snippets.

@maksbd19
Created February 16, 2015 11:24
Show Gist options
  • Save maksbd19/43f0598f28eabf4d5bd1 to your computer and use it in GitHub Desktop.
Save maksbd19/43f0598f28eabf4d5bd1 to your computer and use it in GitHub Desktop.
Assign a default role with the PMPro membership plan
/*
subscriber will be assigned to default role when they sign up
*/
function my_pmpro_after_change_membership_level($level_id, $user_id)
{
if($level_id > 0){
$role = get_option('pmpro_role_for_id_'.$level_id);
$wp_user_object = new WP_User($user_id);
if($role == ''){
$wp_user_object->set_role('subscriber');
}
else{
$wp_user_object->set_role($role);
}
}
}
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
/*
* add the options panel to set a default role
* that is to be added to the user while subscribing
* with the corresponding plan
*
* */
function my_pmpro_membership_level_after_other_settings(){
?>
<h3 class="topborder"><?php _e('Role Settings', 'pmpro');?></h3>
<table class="form-table">
<tbody>
<tr>
<th scope="row" valign="top"><label for="prp-roles"><?php _e('Role', 'pmpro');?>:</label></th>
<td>
<?php
global $wp_roles;
if(isset($_REQUEST['edit'])){
$edit = $_REQUEST['edit'];
$role = get_option('pmpro_role_for_id_'.$edit);
}
else{
$role="";
}
$options = '<option value="-1">Choose Role</option>';
foreach ( $wp_roles->roles as $key=>$value ){
$options .= "<option value=\"{$key}\" " . selected($role, $key, false) . ">{$value['name']}</option>";
}
?>
<select name="default_role" id="prp-roles"><?php echo $options;?></select>
<p class="description">Choose the default Role for this subscription</p>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action('pmpro_membership_level_after_other_settings','my_pmpro_membership_level_after_other_settings');
/*
* save the custom option with user role associated with the current subscription
* plan on saving the corresponding one.
*
* */
function my_pmpro_save_membership_level($id){
if( $id <=0 ){
return;
}
$role = esc_attr($_REQUEST['default_role']);
update_option('pmpro_role_for_id_'.$id, $role );
}
add_action('pmpro_save_membership_level','my_pmpro_save_membership_level');
/*
* delete the custom option that was saved for the current subscription
* plan on the deletion of the corresponding one.
*
* */
function bmc_pmpro_delete_membership_level($id){
if( $id <=0 ){
return;
}
delete_option('pmpro_role_for_id_'.$id);
}
add_action('pmpro_delete_membership_level','bmc_pmpro_delete_membership_level');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment