Disable Admin Bar for Non-Admin Users in WordPress Easily

How to disable admin bar for non-admin users in wordpress; Wordpress hide admin bar for non-admins; Disable wordpress admin bar for non-admin users; Remove admin bar for non-admin users wordpress; Wordpress code to hide admin bar for non-admins; How to hide wordpress admin bar for non-admin users; Wordpress disable admin bar for all users except admins; Hide admin bar wordpress non-admin; Wordpress remove admin bar for non-admin users; Disable admin bar wordpress non-admin;

Explanation

If you're looking to keep the WordPress admin bar out of sight for users who aren't administrators, this snippet is your go-to solution.

What It Does:

  • Checks if the current user is not an administrator.
  • If the user isn't an admin, it hides the admin bar for them.

How It Works:

  • The function current_user_can('administrator') checks if the user has admin privileges.
  • is_admin() ensures the check is only applied on the front end, not in the admin dashboard.
  • show_admin_bar(false) is the command that actually hides the admin bar.

Simply put, this code ensures that only administrators see the admin bar, keeping things tidy for everyone else. Just pop this code into your theme's functions.php file, and you're all set!

Code

<?php
// Function to disable the admin bar for non-admin users
function wp_dudecom_disable_admin_bar_for_non_admins() {
    if (!current_user_can('administrator') && !is_admin()) {
        show_admin_bar(false);
    }
}
add_action('after_setup_theme', 'wp_dudecom_disable_admin_bar_for_non_admins');
?>

Instructions

File Location: functions.php

Prerequisites:

  • Access to your WordPress theme's files, specifically the functions.php file.
  • Basic understanding of how to edit WordPress theme files.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor.
  3. In the right-hand sidebar, find and click on functions.php to open it for editing.
  4. Scroll to the bottom of the functions.php file.
  5. Copy and paste the provided code snippet into the file:
  6. 
    // Function to disable the admin bar for non-admin users
    function wp_dudecom_disable_admin_bar_for_non_admins() {
        if (!current_user_can('administrator') && !is_admin()) {
            show_admin_bar(false);
        }
    }
    add_action('after_setup_theme', 'wp_dudecom_disable_admin_bar_for_non_admins');
    
  7. Click the Update File button to save your changes.
  8. Log out and log back in as a non-admin user to verify that the admin bar is hidden.

If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.