BuddyDev

Search

[Resolved] Disable Admin Bar for All Users Except for Administrators code not working

  • Participant
    Level: Guru
    Posts: 932
    calu on #32250

    Hi there, I have a general WP login issue, which I hope you can help me with.

    I’m using this code snippet below, to disable WP dashboard, for other users than admins.

    Anyway, sometimes users without admin status, are able to log into their dashboard. This happens after a second login attempt with this cookie warning:

    Error: Cookies are blocked or do not understand by your browser. You must enable cookies
    to using WordPress.

    https://imgur.com/gTqUE0f
    https://imgur.com/m9kAV6x

    Here’s the code snippet I have added to avoid dashboard access for others than admins:

    //Disable Admin Bar for All Users Except for Administrators
    add_action( 'after_setup_theme', 'remove_admin_bar' );
    
    function remove_admin_bar() {
    	if ( ! current_user_can( 'administrator' ) && ! is_admin() ) {
    		show_admin_bar( false );
    	}
    }
    

    https://stackoverflow.com/questions/8194649/disable-the-admin-bar-for-all-users-except-admin

    How can this happen anyway?

    Thanks!

    Regards
    Carsten

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 1817
    Ravi on #32260

    Hello Carsten,

    Please try the following code. If you want to hide admin bar from non administrator and do not want to give access them to dashboard.

    
    // To hide admin bar to non administrator.
    add_action('after_setup_theme', function(){
    	if ( is_super_admin() ) {
    		return;
    	}
    
    	show_admin_bar( false );
    
    	if ( is_admin() && ! wp_doing_ajax() ) {
    		wp_safe_redirect( home_url() );
    		exit;
    	}
    });
    
    

    Please do let me know if it works or not.

    Regards
    Ravi

    • This reply was modified 1 year, 2 months ago by Ravi.
  • Participant
    Level: Guru
    Posts: 932
    calu on #32267

    Hi Ravi, thanks very much for your prompt reply and a code solution.

    Unfortunately the code breaks my site, would you please check again for a syntax error?

    Thanks!

    Carsten

  • Keymaster
    (BuddyDev Team)
    Posts: 19487
    Brajesh Singh on #32274

    Hi Carsten,
    Ravi is away, so I am updating the code.

    Please use this snippet

    
    // To hide admin bar to non administrator.
    add_action( 'init', function () {
    	if ( is_super_admin() ) {
    		return;
    	}
    
    	show_admin_bar( false );
    
    	if ( is_admin() && ! wp_doing_ajax() ) {
    		wp_safe_redirect( home_url( '/' ) );
    		exit;
    	}
    } );
    

    Does it work fro you?

  • Participant
    Level: Guru
    Posts: 932
    calu on #32275

    Hi Ravi, found this snippet which works as well:

    add_action('after_setup_theme', 'remove_admin_bar');
    function remove_admin_bar() { if (!current_user_can('administrator') && !is_admin()) { show_admin_bar(false); } }

    Thanks!

  • Keymaster
    (BuddyDev Team)
    Posts: 19487
    Brajesh Singh on #32276

    Hi,
    Avoid using ‘after_setup_theme’. The current user is not set at this hook, so it should not be used.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 932
    calu on #32279

    Hi Brajesh, thanks again, for your expert advice regarding the other code.

    I have pasted your updated code in now, and it is working perfectly!

    Thanks again, to both of you!

    Regards
    Carsten

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 1817
    Ravi on #32289

    Hello Carsten,

    Thank you for the acknowledgement.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved