Created
December 19, 2022 11:26
-
-
Save champsupertramp/54b822a7ba4239ad17dfb9461fe742d0 to your computer and use it in GitHub Desktop.
Ultimate Member - Restrict Login form for specific role
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( 'administrator' !== um_user('role') ){ | |
UM()->form()->add_error( 'user_email', __( 'Login form restricted', 'ultimate-member' ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment