DEV Community

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

Posted on • Edited on • Originally published at w3courses.org

9 1

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:)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay