[Resolved] Roles in register form according to option select


  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #28401

    Hi there! How can I do so that according to the option chosen in the register form set the role? Try several ways, but it doesn’t work. My last code is this:

    Any suggestions? Thanks!

    <label><input type=”radio” name=”post-format” value=”ans1″ >Editor</label>
    <label><input type=”radio” name=”post-format” value=”ans2″ id=”post-format-gallery”><Redactor</label>

    $answer = $_POST[‘post-format’];
    if ($answer == “ans1”) {
    wp_insert_user( array ( ‘role’ => ‘editor’) );

  • Keymaster
    (BuddyDev Team)
    Posts: 22574
    Brajesh Singh on #28408

    Hi Jennifer,
    Thank you for the question.

    Your best bet is to do it on ‘bp_core_activated_user’. Check for the xprofile field and user set_role() over the user object to set role.

    Here is an example, Please feel free to customize according to your needs.

    add_action( 'bp_core_activated_user', function ( $user_id ) {
    	$user     = get_user_by( 'id', $user_id );
    	$field_id = 20; // Numeric field id.
    	// We need raw data not in displayable form, so I am not using xprofile_get_field_data
    	$field_val = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $field_id, $user_id ) );
    	// example of setting role.
    	if ( 'ans1' == $field_val ) {
    		$user->set_role( 'subscriber' );
    }, 1 );


You must be logged in to reply to this topic.

This topic is: resolved