Snippet

Hide WordPress Admin Panel for Subscribers Easily

How to hide wordpress admin bar for subscribersDisable admin bar for non-admin users wordpressWordpress hide admin panel for subscribersRemove wordpress admin bar for subscribersWordpress disable dashboard access for subscribersHide admin bar for specific user roles wordpressWordpress restrict admin bar to admins onlyHow to disable wordpress admin bar for certain usersWordpress hide admin bar from subscribersDisable wordpress admin bar for non-admins

Explanation

If you're looking to keep things tidy for your subscribers by hiding the admin bar and restricting dashboard access, here's how this code helps:

  • Hide Admin Bar: The code checks if a user can edit posts. If they can't (like subscribers), it hides the admin bar for them. This keeps the interface clean and simple for those who don't need access to admin features.
  • Restrict Dashboard Access: It also prevents subscribers from accessing the WordPress dashboard. If they try to visit it, they'll be redirected to your site's homepage instead. This ensures that only users with higher permissions can access the backend.

In short, this code is perfect for keeping your WordPress site streamlined for subscribers, letting them focus on the content without unnecessary distractions.

Code

1<?php 2// Function to hide the WordPress admin bar for subscribers 3function wp_dudecom_hide_admin_bar_for_subscribers() { 4 if (!current_user_can('edit_posts')) { 5 add_filter('show_admin_bar', '__return_false'); 6 } 7} 8add_action('after_setup_theme', 'wp_dudecom_hide_admin_bar_for_subscribers'); 9 10// Function to restrict dashboard access for subscribers 11function wp_dudecom_restrict_dashboard_access() { 12 if (is_admin() && !current_user_can('edit_posts') && !(defined('DOING_AJAX') && DOING_AJAX)) { 13 wp_redirect(home_url()); 14 exit; 15 } 16} 17add_action('admin_init', 'wp_dudecom_restrict_dashboard_access'); 18?>

Instructions

To implement the code that hides the admin panel for subscribers, follow these steps:

File Location: You will need to add the code to your theme's functions.php file or a custom plugin file.

Prerequisites: Ensure you have access to your WordPress site's file system, either via FTP or a file manager provided by your hosting service.

Implementation Steps:

  1. Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation files.
  2. Navigate to Your Theme Directory: Go to wp-content/themes/your-active-theme/ and locate the functions.php file.
  3. Edit the Functions File: Open the functions.php file in a text editor.
  4. Add the Code: Copy and paste the provided code snippet at the end of the functions.php file.
  5. Save Changes: Save the functions.php file and upload it back to the server if using FTP.
  6. Test the Implementation: Log in as a subscriber to ensure the admin bar is hidden and dashboard access is restricted.

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