• Resolved chanchan02

    (@chanchan02)


    Is it possible to have a separate log-in form for each role with a filter to only allow specific user roles?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @chanchan02

    You can try the following code snippets to restrict the Login form for specific role. You can add this to your theme/child-theme’s functions.php file or use the Code Snippets plugin to run the code:

    add_action( 'um_submit_form_errors_hook_login', 'um_071621_login_for_specific_role', 10 );
    function um_071621_login_for_specific_role( $args ){
    
        if ( isset( $args['username'] ) && $args['username'] == '' ) {
            return;
        }
    
    	if ( isset( $args['user_login'] ) && $args['user_login'] == '' ) {
            return;
        }
    
    	if ( isset( $args['user_email'] ) && $args['user_email'] == '' ) {
            return;
        }
        
    	$user_id = null;
    	if ( isset( $args['username'] ) ) {
    		if ( is_email( $args['username'] ) ) {
    			$data = get_user_by('email', $args['username'] );
    			$user_id = isset( $data->ID ) ? $data->ID : null;
    		}else{
                $data = get_user_by( 'login', $args['username'] );
                $user_id = isset( $data->ID ) ? $data->ID : null;
            }
    	} elseif ( isset( $args['user_email'] ) ) {
    		if ( is_email( $args['user_email'] ) ) {
    			$data = get_user_by('email', $args['user_email'] );
    			$user_id = isset( $data->ID ) ? $data->ID : null;
    		}else{
                $data = get_user_by( 'login', $args['user_email'] );
                $user_id = isset( $data->ID ) ? $data->ID : null;
            }
        } elseif( isset( $args['user_login'] ) ){
            if ( is_email( $args['user_login'] ) ) {
    			$data = get_user_by('login', $args['user_login'] );
    			$user_id = isset( $data->ID ) ? $data->ID : null;
    		}else{
                $data = get_user_by( 'login', $args['user_login'] );
                $user_id = isset( $data->ID ) ? $data->ID : null;
            }
        }
        um_fetch_user( $user_id );
    
        if( isset( $args['form_id'] ) && 6 == $args['form_id'] ){
            if( 'administrator' !== um_user('role') ){
                UM()->form()->add_error( 'user_email', __( 'Login form restricted', 'ultimate-member' ) );
            }
        }
    }

    The above will restrict the Login form with ID#6 for Administrator only. You have to change the ID#6 with your Login form ID. Also, you need to change the administrator in the condition to assign the form to that user role.

    If you need to apply these to multiple Login forms, you can try the following format:

    if( isset( $args['form_id'] ) && 6 == $args['form_id'] ){
            if( 'administrator' !== um_user('role') ){
                UM()->form()->add_error( 'user_email', __( 'Login form restricted', 'ultimate-member' ) );
            }
    }elseif( isset( $args['form_id'] ) && 7 == $args['form_id'] ){
            if( 'subscriber' !== um_user('role') ){
                UM()->form()->add_error( 'user_email', __( 'Login form restricted', 'ultimate-member' ) );
            }
    }

    Regards,

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @chanchan02

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. ??

    Regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Does Login with User Role Filter available?’ is closed to new replies.