WordPress.org

Ready to get started?Download WordPress

Forums

Theme My Login
Register to a different Role (3 posts)

  1. futurenet
    Member
    Posted 10 months ago #

    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

    http://wordpress.org/plugins/theme-my-login/

  2. futurenet
    Member
    Posted 10 months ago #

    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

  3. rbriones
    Member
    Posted 5 months ago #

    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.

    http://wordpress.org/support/topic/add-user-roles-during-registration?replies=2

Reply

You must log in to post.

About this Plugin

About this Topic