• Hi all,
    I’ve been trying to figure out how to modify the coding (or add a new bit) for registrations to be assigned to a specific, different to default, role.

    e.g. I have a role called “Guest” (default) and have created another role called “Friends”

    How can one register and be assigned to “Friends” role?

    Thanks and needless to say, GREAT PLUGIN

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter futurenet

    (@futurenet)

    Contribution to community:

    This is how I solved the issue (given that in TML > Moderation > “E-mail Confirmation” is choosen):

    1. Insert this code in functions.php

    function set_role_on_registration( $user_id ) {
    
    		if( isset($_POST['role']) )
    			{
    				$role = 'friends';
    			}
    
    	if ( in_array( $role, array( 'friends', 'guest' ) ) )
    		add_user_meta( $user_id, 'pending_role', $role );
    }
    add_action( 'tml_new_user_registered', 'set_role_on_registration' );
    
    function set_role_on_activation( $user_id ) {
    	if ( $pending_role = get_user_meta( $user_id, 'pending_role', true ) ) {
    
    		if ( !in_array( $pending_role, array( 'friends', 'guest' ) ) )
    			return;
    
    		$user = new WP_User( $user_id );
    
    		$user->set_role( $pending_role );
    
    		unset( $user );
    
    	}
    }
    add_action( 'tml_new_user_activated', 'set_role_on_activation' );

    2. Copy theme-my-login/templates/login-form.php to your current theme’s directory and rename it login-form-2.php
    Create a new page titled “Login Form 2” and place in it the following shortcode: [theme-my-login login_template=”login-form-2.php”]

    3. Edit file login-form-2.php and enter the following code:

    <input type="hidden" name="role" value="friends" />

    just under line:

    <p class="submit">

    4. In front-end, role-based registration is made through page “Login Form 2”

    Cheers

    rbriones

    (@rbriones)

    This is somewhat what I am looking for too except that my requirement does not require email confirmation.

    Digging more on the support section, I found a similar question which was resolved by Jeff himself.

    https://www.ads-software.com/support/topic/add-user-roles-during-registration?replies=2

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Register to a different Role’ is closed to new replies.