Technology Magnet

Discover, Learn, and Thrive in the World of Technology

Disable Admin Bar for All Users Except Administrators Using Code

July 25, 2023 1 Min read TUTORIALS
disable-wordpress-admin-bar

To 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.



Author: Aminul Islam

Aminul Islam covers startups, online money earning, web hosting & domain, ecommerce, tech policy-related updates, and tech gadgets reviews. He is expert on web development.

More Topics:
Code: Get Article Reading Time using PHP − To calculate the reading time of an article using PHP, you can use the following PHP code: In the above code, the calculateReadingTime function takes.

Comments