• Hello!
    Thank you for great plugin!
    I’m trying to add Roles to Registration form like describe here https://www.jfarthing.com/development/theme-my-login/adding-extra-registration-fields/

    register-form.php

    <p>
    			<label for="role<?php $template->the_instance(); ?>"><?php _e( 'Role*', 'theme-my-login' ) ?></label>
    			<select name="role" id="role<?php $template->the_instance(); ?>" class="input">
    				<option value="role1">Role 1</option>
    				<option value="role2">Role 2</option>
    			</select>
    		</p>

    and
    theme-my-login-custom.php

    function tml_registration_errors( $errors ) {
    	if ( empty( $_POST['user_role'] ) )
    		$errors->add( 'empty_user_role', '<strong>ERROR</strong>: Please select your role.' );
    	return $errors;
    }
    add_filter( 'registration_errors', 'tml_registration_errors' );
    
    function tml_user_register( $user_id ) {
    	if ( !empty( $_POST['role'] ) )
    		update_user_meta( $user_id, 'role', $_POST['role'] );
    }
    add_action( 'user_register', 'tml_user_register' );

    At WP Settings I have default registration role is Role1. But after I added additional fields to registration form and select Role 2 during registration I still have Role 1 after registration is complete.

    I will be appreciate for any help.

    https://www.ads-software.com/plugins/theme-my-login/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    Not 100% sure but I think you need the following:
    wp_update_user and not update_user_meta.

        if (!empty( $_POST['role'])) wp_update_user(array(
            'ID' => $user_id,
            'role' => $_POST['user_role']
        ));

    Hi,
    Not 100% sure but I think you need to wp_update_user and not update_user_meta

            if (!empty($_POST['role'])) wp_update_user(array(
                'ID' => $user_id,
                'role' => $_POST['role']
            ));
    Thread Starter M@X

    (@cityz)

    No, it’s doesn’t work. I tried and wp_update_user and:

    
    $u = new WP_User(  $user_id );
    $u->remove_role( 'role1' );
    $u->add_role( 'role2' );
    

    both not working ??

    Thread Starter M@X

    (@cityz)

    For temporary suggestion I created additional field in profile, call it “Selected Role” and putting there what user selected during registration:

    
    update_user_meta( $user_id, 'selected_role', $_POST['role'] );
    

    and than manually checking and updating the role.
    It’s little bit stupid but works for now ??

    Hi,
    I think I found your (and mine now) problem.
    Have you set the user to activate account via email?

    If so, this is the issue, regardless of what role they choose, standard WordPress action after activation is to set the role to whatever you have chosen in the admin settings “New User Default Role”.

    This is happening to me know after I set “E-mail Confirmation” in the Moderation module.

    I’m thinking maybe to add a temporary field in the usermeta and then add a function to the “wpmu_activate_user” hook once activation is complete to pull this user meta and update role to what they originally chose.

    If I manage to sort out will post here.

    Malisa

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add Roles During Registration’ is closed to new replies.