BuddyDev

Search

Setting Member Types (with Members Types Pro) on user-new.php form

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #18887

    Hi there,

    I’ve created a xprofile single member type field using the instructions found in the plugin’s documentation section. I’ve added it to the user-new.php registration form as a dropdown option with the assumption that an admin or editor (I’ve modified the editor role for create user capabilities) can set the member type when creating a new user. The user is being created but the member type is not being set. Can someone have a look at the following code and let me know where I’m going wrong?

    //  add fields to new user form
    add_action( 'user_new_form', 'mktbn_user_new_form' );
    
    function mktbn_user_new_form()
    {
    ?>    
    
    <table class="form-table">
        <tbody>
        <tr class="form-field">
            <th scope="row"><label for="Member type">Community Member Type</label></th>
            <td>
      <select name="MemberType">
      	<option value="type1">Member Type 1</option>
      	<option value="type2">Member Type 2</option>
     	<option value="type3">Member Type 3</option>
      </select>
           </td>
        </tr>    
    
        </tbody>
    </table>
    
    <?php   
    }
    
    //  process the extra fields for new user form
    add_action( 'user_register', 'mktbn_user_register', 10, 1 );
    
    function mktbn_user_register( $user_id )
    {
        mikakoo_log( 'user_register: ' . $user_id );
    
        {
            update_user_meta($user_id, 'member type', $_POST['MemberType']);
        }
    }
    
  • Keymaster
    (BuddyDev Team)
    Posts: 24238
    Brajesh Singh on #18888

    Hi,
    Xprofile fields are not supported on Dashboard->Add User screen.

    In this case, you can change the following line

    
    update_user_meta($user_id, 'member type', $_POST['MemberType']);
    
    

    with this

    
    if ( ! empty( $_POST['MemberType'] ) ) {
    	bp_set_member_type( $user_id, $_POST['MemberType'] );
    }
    
    

    That will make it work.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #18908

    Hi there, thank you for your reply! unfortunately this code seems to generate a http 500 internal error. The user gets created, then the page refreshes to internal 500 error, and the member type is not assigned. Any thoughts?

  • Keymaster
    (BuddyDev Team)
    Posts: 24238
    Brajesh Singh on #18909

    Hi Jeff,
    Please share the complete code for function mktbn_user_register()

    Most probably there is some syntaxt error causing it.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #18911

    Thank you sir!

    
    //  add fields to new user form
    add_action( 'user_new_form', 'mktbn_user_new_form' );
    
    function mktbn_user_new_form()
    {
    ?>    
    
    <table class="form-table">
        <tbody>
        <tr class="form-field">
            <th scope="row"><label for="Member type">Community Member Type</label></th>
            <td>
      <select name="MemberType">
      	<option value="type1">Member Type 1</option>
      	<option value="type2">Member Type 2</option>
     	<option value="type3">Member Type 3</option>
      	
      </select>
           </td>
        </tr>    
    
        </tbody>
    </table>
    
    <?php   
    }
    
    //  process the extra fields for new user form
    add_action( 'user_register', 'mktbn_user_register', 10, 1 );
    
    function mktbn_user_register( $user_id )
    {
        mikakoo_log( 'user_register: ' . $user_id );
    
            if ( ! empty( $_POST['MemberType'] ) ) {
    	bp_set_member_type( $user_id, $_POST['MemberType'] );
    		}
        
    }
    
  • Keymaster
    (BuddyDev Team)
    Posts: 24238
    Brajesh Singh on #18912

    HI Jeff,
    Thank you. Your code is correct. If it is giving 500 error, It means one of the used function does not exist.

    I have slightly modified the function, Please change the function with this one and then let me know if it works or not?

    
    
    function mktbn_user_register( $user_id )
    {
    	if ( function_exists( 'mikakoo_log' ) ) {
    		mikakoo_log( 'user_register: ' . $user_id );
    	}
    
    	if ( ! empty( $_POST['MemberType'] ) && function_exists( 'bp_set_member_type' ) ) {
    		bp_set_member_type( $user_id, $_POST['MemberType'] );
    	}
    
    }
    

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #18915

    Looks like that worked! One more quick question though, I have created custom roles that match each member type name exactly. Can you recommend an additional piece of code that will set the new user’s role to match the name of the selected member type?

  • Keymaster
    (BuddyDev Team)
    Posts: 24238
    Brajesh Singh on #18916

    Sure, Here is the functiona updated again.

    
    
    function mktbn_user_register( $user_id )
    {
    	if ( function_exists( 'mikakoo_log' ) ) {
    		mikakoo_log( 'user_register: ' . $user_id );
    	}
    
    	if ( ! empty( $_POST['MemberType'] ) && function_exists( 'bp_set_member_type' ) ) {
    		bp_set_member_type( $user_id, $_POST['MemberType'] );
    	}
    
    	$user = get_user_by( 'id', $user_id );
    
    	// if member type submitted and valid user and the person adding user is super admin
        // then set role too.
    	if ( $user && ! empty( $_POST['MemberType'] ) && is_super_admin() ) {
    	    $user->set_role($_POST['MemberType'] );
    	}
    
    }
    
    

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #18927

    Thank you! As I’d mentioned before, these new users will be added by a user with role “editor” so I assume I can remove the is_super_admin criteria from the if statement for this to work properly?

    Thanks!

  • Keymaster
    (BuddyDev Team)
    Posts: 24238
    Brajesh Singh on #18930

    Hi Jeff,
    Yes, that is correct.

    I added the check as we do not want users to misuse it. The user_register action fires even when a user registers from front end and if we do not put a check on who is creating the user, The code has a potential to be misused.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved