Support

Account

Home Forums General Issues Deny user login/redirect based on ACF values

Solved

Deny user login/redirect based on ACF values

  • Hello there,

    We have created and attached custom status field for all users and I would like to find out if there’s a way to prevent users with certain statuses logging in. For example, I would like to deny the logging in feature to users whose status is ‘disabled’ or ‘pending’ when they try to log in and redirect them to home page or elsewhere. I am pretty sure someone must have asked this before but so far no luck finding a working solution.

    Thanks and regards,

  • You are looking to use this hook https://developer.wordpress.org/reference/hooks/login_redirect/.

    There are examples on that page.

    The hook passes the user data, you use that to get the field from the user and redirect based on the field value

    
    $value = get_field('field_name', $user);
    
  • Many thanks John! For anyone looking for a similar functionality, this is what I did and it worked well.

    function pm_login_redirect( $redirect_to, $request, $user ) {
    	$redirect_to =  '/dashboard';
    
    	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    		//check for subscribers
    		if ( in_array( 'subscriber', $user->roles ) ) {
    			//check the user status
    			$userstatus = get_field('Status', $user);
    			
    			if ($userstatus ==='Disabled') {
    				wp_logout();
    				wp_redirect('https://www.google.com' );
    				exit();
    			}
    		} else {
    			// redirect the other subscribers to the default place
    			return $redirect_to;
    		}
    	}
    }
    
    add_filter( 'login_redirect', 'pm_login_redirect', 10, 3 );
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.