Bird
0
0

How can you disable the WordPress admin bar for all users except administrators by adding code to functions.php?

hard📝 Application Q9 of 15
Wordpress - Theme Structure and Basics
How can you disable the WordPress admin bar for all users except administrators by adding code to functions.php?
A<pre>add_filter('show_admin_bar', '__return_false');</pre>
B<pre>add_action('after_setup_theme', function() { if (!current_user_can('administrator')) { show_admin_bar(false); } });</pre>
C<pre>remove_action('wp_head', 'wp_admin_bar_render');</pre>
D<pre>if (is_user_logged_in()) { show_admin_bar(false); }</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct hook and condition

    Using after_setup_theme ensures the code runs early. Checking current_user_can('administrator') targets admin users.
  2. Step 2: Use show_admin_bar(false) to disable the admin bar for non-admins

    This disables the admin bar only for users without administrator capability.
  3. Step 3: Review incorrect options

    add_filter('show_admin_bar', '__return_false');
    disables admin bar for all users, C removes rendering but is incomplete, D disables for all logged-in users.
  4. Final Answer:

    add_action('after_setup_theme', function() { if (!current_user_can('administrator')) { show_admin_bar(false); } }); -> Option B
  5. Quick Check:

    Disable admin bar conditionally with current_user_can [OK]
Quick Trick: Use current_user_can to conditionally disable admin bar [OK]
Common Mistakes:
  • Disabling admin bar for all users
  • Not using proper hook for capability checks
  • Removing admin bar rendering incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes