BuddyDev

Search

Avoid members to enter their WP dashboard


  • Participant
    Level: Guru
    Posts: 731
    calu on #21398

    Hi there, I have a WP login issue, which I am trying to solve.

    If a user, already logged in, returns to my front page and presses the domain/login link again, the user is directed to their WP dashboard.

    Why is this happening, and how do I avoid this to happen, other than maybe hiding the login button for logged in users?

    Regards
    Carsten


  • Keymaster
    (BuddyDev Team)
    Posts: 16424
    Brajesh Singh on #21448

    Hi Carsten,
    You don’t have to hide the login button. It is very easy to restrict users from vising dashboard.

    You may use something like this in your theme’s functions.php

    
    
    /**
     * Protect dashboard access.
     */
    function budddev_protect_dashboard() {
    	// do not prevent ajax requests.
    	if( defined( 'DOING_AJAX') ) {
    		return;
    	}
    
    	$allowed_roles = array('administrator', 'editor');
    
    	// if user is not logged i
    	// or does not have one of any of the allowed roles.
    	// redirect.
    	if ( ! is_user_logged_in() || ! array_intersect( wp_get_current_user()->roles, $allowed_roles ) ) {
    
    		wp_safe_redirect( site_url('/') );
    		exit(0);
    	}
    
    }
    add_action( 'admin_init', 'budddev_protect_dashboard' );
    
    

    Regards
    Brajesh


  • Participant
    Level: Guru
    Posts: 731
    calu on #21450

    Hi Brajesh, thank you very much for the code

    Regards
    Carsten


  • Keymaster
    (BuddyDev Team)
    Posts: 16424
    Brajesh Singh on #21464

    You are welcome.

    Regards
    Brajesh

The topic ‘Avoid members to enter their WP dashboard’ is closed to new replies.

This topic is: not resolved
Subscribe