Disable Admin Bar for All Users Except Administrators Using Code
July 25, 2023 ⚊ 1 Min read ⚊ TUTORIALSTo disable the admin bar for all users except administrators in WordPress, you can add the following code to your theme’s functions.php file or in a custom plugin:
function disable_admin_bar_for_non_admins() {
// Check if the current user is an administrator
if (!current_user_can('administrator') && !is_admin()) {
// Disable the admin bar for non-admin users
show_admin_bar(false);
}
}
add_action('after_setup_theme', 'disable_admin_bar_for_non_admins');
This code will hook into the after_setup_theme action, which runs after the theme is set up, and it will check if the current user is not an administrator and is not viewing the admin area. If those conditions are met, it will hide the admin bar for that user.
Remember to always make a backup of your files before making changes to the theme’s functions.php file or any other core files. Additionally, if you’re not familiar with modifying code, consider using a child theme or a custom plugin to add this code, as it will prevent your changes from being lost during theme updates.