Wordpress disable admin bar to users except admin
To disable the admin bar for users except the admin in WordPress, you can follow these steps:
- Open your WordPress theme's
functions.php
file. - Locate the code that initializes your theme, which often begins with
function theme_name_setup() {
. - Add the following code within the
theme_name_setup
function:
if (!current_user_can('administrator')) {
show_admin_bar(false);
}
Let's go through each step in more detail:
Step 1: Open functions.php
file
First, you need to locate and open the functions.php
file for your WordPress theme. This file is responsible for defining the functions and features of your theme.
Step 2: Locate theme initialization code
Within the functions.php
file, you'll find a section of code that initializes your theme. This code typically starts with a function named something like theme_name_setup()
. You need to find this part of the code.
Step 3: Add the code to disable admin bar
Inside the theme_name_setup
function, you can add the following code:
if (!current_user_can('administrator')) {
show_admin_bar(false);
}
This code checks if the current user is not an administrator using the current_user_can('administrator')
function. If the user is not an admin, it calls the show_admin_bar(false)
function to disable the admin bar for that user.
Once you have added this code to the functions.php
file, save the changes. The admin bar will now be disabled for all users except administrators.
It's important to note that modifying the theme's functions.php
file directly is not recommended, as any future theme updates may overwrite your changes. Instead, you should consider using a child theme or a custom plugin to add this code.