Rremove adminbar for non-admin users

Home » Snippets » Rremove adminbar for non-admin users
0

Created with:

Visibility: 

public

Creator: Ahrale

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Remove admin bar for non-admin users.
 */
function atar4u_remove_admin_bar() {
    if ( ! current_user_can( 'manage_options' ) ) {
        show_admin_bar( false );
    }
}
add_action( 'after_setup_theme', 'atar4u_remove_admin_bar' );
				

Explanation:

The code snippet above defines a function called atar4u_remove_admin_bar which checks if the current user does not have the capability to manage options (i.e., is not an admin). If the user is not an admin, the show_admin_bar function is called with the parameter false to hide the admin bar.

The add_action function is then used to hook the atar4u_remove_admin_bar function to the after_setup_theme action, ensuring that the admin bar is removed for non-admin users when the theme is set up.

Register an account to save your snippets or go Pro to get more features.