DEV Community 👩‍💻👨‍💻

Cover image for Remove Admin Bar in Wordpress
Chetan Rohilla
Chetan Rohilla

Posted on • Originally published at readymadecode.com

Remove Admin Bar in Wordpress

Sometimes, we need to remove admin bar in our wordpress website to make some extra security. To remove admin bar for non admin users, paste this code in your functions.php file located at root_directory_path/wp-content/themes/your_theme/functions.php

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
    if (!current_user_can('administrator') && !is_admin()) {
        show_admin_bar(false);
    }
}
Enter fullscreen mode Exit fullscreen mode

And paste this code in your functions.php file located at root_directory_path/wp-content/themes/your_theme/functions.php

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
    show_admin_bar(false);
}
Enter fullscreen mode Exit fullscreen mode

If you want to remove admin bar for shop manager in your woocommerce website. Then paste this code in your functions.php file located at root_directory_path/wp-content/themes/your_theme/functions.php

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
    if ( isset( $user['roles'][0] ) && $user['roles'][0] == 'shop_manager' ) {
        show_admin_bar(false);
    }
}
Enter fullscreen mode Exit fullscreen mode

If you want to remove admin bar for other users roles. Then here below is trick which you can use to identify the user roles and if condition in remove_admin_bar() function. Here $user_roles is an array having all roles values of wordpress users. And in $user_roles you can find your desired roles and can apply condition to your desired roles.

$user = wp_get_current_user();
$user_roles = (array) $user->roles;
if ( in_array( 'author', $user_roles ) ) {
    //The user has the "author" role
}
Enter fullscreen mode Exit fullscreen mode

Please like share subscribe and give positive feedback to motivate me to write more for you.

For more tutorials please visit my website.

Thanks:)
Happy Coding:)

Top comments (0)

What image format should you use in your next project? 🤔