Skip to main content

Disable Admin Bar

PropertyValue
descriptionDisable admin bar for non-admin users on the frontend.
tagslib, php, wp, oop
rating

Overview

Disable the WordPress admin bar on the frontend for users who do not pass the configured administrator check.

The snippet runs on after_setup_theme, leaves the admin bar untouched inside wp-admin, and keeps it visible for users who satisfy current_user_can( 'administrator' ). It is useful on membership or client-facing sites where logged-in non-admin users should not see the toolbar.

Variants

<?php
/**
* Maybe disable admin bar.
*/
if ( ! function_exists( 'mac_maybe_disable_admin_bar' ) ) {

function mac_maybe_disable_admin_bar(): void {

// Keep admin bar in wp-admin.
if ( is_admin() ) {
return;
}

// Allow administrators.
if ( current_user_can( 'administrator' ) ) {
return;
}

show_admin_bar( false );
}
}

add_action( 'after_setup_theme', 'mac_maybe_disable_admin_bar', 10 );