Complete Guide to Hide Menu Items Based on Roles in WordPress

In WordPress, managing the user experience on the backend is just as important as managing the front end. For websites with multiple contributors, administrators often need to control what users can see and access in the WordPress dashboard. That’s where the feature of hiding menu items becomes valuable.

It lets you hide menu items based on the role users have. To help you achieve the same, we’ll dive into two methods: using plugins and using custom code to hide menu items. We’ll also have a look at the best practices WordPress development experts follow while hiding menu items. But before that, let’s understand the reason behind doing so.

Why Hide Menu Items Based on User Roles?

Hiding menu items based on user roles in WordPress can provide several significant benefits, both for the user experience and the administrators. Here are some key benefits that may compel you to implement this feature:

  • Enhanced Security: By limiting the menu items visible to each user role, you can prevent unauthorized access to sensitive areas of your website. A cleaner menu reduces the potential entry points for cyber attacks.
  • Improved User Experience: Users only see the options relevant to their role, making navigation more simple and efficient. Plus, easy navigation can help users find what they are looking for easily.
  • Compliance with Regulations: In some cases, hiding certain menu items may be necessary to comply with data privacy regulations like GDPR. Hiding menu items based on roles ensures that users only have access to the features they need, reducing the security risk.

These benefits are the reason why you may want to hide menu items based on the role. Now that you know the importance of hiding menu items, let’s begin with how you can hide them.

How to Hide Menu Items Based on Role in WordPress?

Hiding menu items based on user roles in WordPress can be done using two main methods: using plugins and custom coding. Both approaches allow you to control menu visibility and ensure that users only see items relevant to their roles. Here’s a stepwise guide to do so:

Method 1: Using Plugin

If you’re not comfortable with coding or want a simpler solution, several plugins are available that allow you to hide menu items. Here, we are going to use the User Role Editor plugin to do so:

Step 1: Go to your WordPress admin dashboard, navigate to Plugins > Add New, and search for the “User Role Editor” plugin. Install the plugin and activate it.

Step 2: Once activated, navigate to Users > User Role Editor. You’ll see a list of all user roles in WordPress. Select the role you want to edit. For example, you might want to restrict certain roles like Editor or Author from viewing specific menu items.

Step 3: After you have selected the role, now you can set the permission. To do that, select which permission you want to edit. But ensure that, to hide a menu item in WordPress, you’ll need to have the Core option selected.

Step 4: Now, choose the abilities the user will have by clicking the checkbox adjacent to various options.

Step 5: Choose the menu items you want to hide. For example, you can click the option to remove installing plugins and themes to prevent others from changing those elements. Similarly, you can choose other menu items you want to hide.

Step 6: Once you have selected all the menus you want to hide based on role, click on the Update button.

Step 7: After you click on the Update button, you get a pop up for the confirmation. Click Yes if you are sure about hiding menu items based on role selected.

Now, the selected menu items based on role won’t be visible to that user. This customization lets administrators set the correct abilities as needed by various users. Now, let’s check out how one can hide menu items based on role using custom code.

Method 2: Using Custom Code

For developers or advanced users, adding custom code to WordPress allows you to have precise control over which menu items. This method requires modifying the functions.php file in your theme. Here is how you can do it:

Step 1: Navigate to Appearance > Theme File Editor in your WordPress dashboard and locate the functions.php file under the active theme.

Step 2: Use the hide_admin_menu_items() function in WordPress, which allows you to hide specific menu items for different user roles. Implement logic using wp_get_current_user() to check the user role and conditionally hide items.

Step 3: To have a reference, here is a sample code that you may add to your function.php code:

function hide_admin_menu_items() {

    $current_user = wp_get_current_user();

    if (in_array('subscriber', $current_user->roles)) {

        remove_menu_page('edit.php'); // Posts

        remove_menu_page('upload.php'); // Media

        remove_menu_page('link-manager.php'); // Links

        remove_menu_page('edit-comments.php'); // Comments

        remove_menu_page('themes.php'); // Appearance

        remove_menu_page('plugins.php'); // Plugins

        remove_menu_page('users.php'); // Users

        remove_menu_page('tools.php'); // Tools

        remove_menu_page('options-general.php'); // Settings

    }

}

add_action('admin_menu', 'hide_admin_menu_items');

Step 4: Once you have added the custom code, save the changes made to the functions.php file.

Step 5: Now, to test that everything is working as expected, log in as a user with the targeted role and verify that the menu items are hidden as intended. If needed, adjust the code to hide or show additional menu items for different roles.

Hiding menu items manually provides professional developers with more control over the permission and abilities. That makes the administration more secure, as only verified users have the right permission as per they needed. To get your site secured with custom permission with robust performance, consider hiring WordPress developers.

Looking to enhance security of your WordPress administration?

Best Practices for Role-Based Menu Customization

We have seen two methods to hide menu items based on role in the previous section. Now let’s find out the best practices you can follow to hide menu items:

  • Use Plugins for Simplicity: If you prefer not to deal with custom code or need a quicker solution, plugins are easier to manage and often offer additional options.
  • Test Thoroughly: Always test your changes by logging in as different user roles to ensure that menu items are hidden as expected.
  • Backup Before Coding: If you’re adding custom code, ensure you backup your site before making changes to the functions.php file to avoid any disruptions.
  • Monitor Performance: Adding too many plugins or complex code can impact site performance. Ensure that your method of hiding menu items does not slow down your website.

By following these best practices, you will be able to hide menu items effectively and easily. Now, let’s find out some of the alternatives WordPress development services use to hide menu items based on their role in WordPress.

Alternate Plugins to Hide Menu Items in WordPress

There are various WordPress plugins available that lets you manage user roles and permissions. Here are some of the plugins among them that allows you to hide menu items based on role in WordPress.

Admin Menu Editor

This plugin allows you to manually edit the WordPress admin menu by rearranging, renaming, hiding, or disabling menu items. It offers a user-friendly interface where you can customize menus for specific roles and adjust permissions.

Features:

  • Customize menus for specific user roles.
  • Change menu icons, sub-menu items, and permissions.
  • Create custom menus or modify existing ones.

Best For: Administrators who want full control over the admin menu and its structure.

Adminimize

Adminimize is designed to provide detailed control over the WordPress backend. It allows you to hide unnecessary items for specific user roles.

Features:

  • Customize the entire WordPress admin interface.
  • Hide admin bar items, meta boxes, and dashboard widgets based on user roles.
  • Hide specific features of WordPress for different users.

Best For: Users who want more comprehensive control over what elements are displayed on the admin dashboard.

Members

Members is a role and capability management plugin that provides control over user permissions, including the visibility of menu items.

Features:

  • Create and manage custom roles.
  • Customize permissions for each role, including menu visibility.
  • Works well with other plugins for more granular control over user capabilities.

Best For: Websites that need flexible role and permission management alongside menu visibility control.

WPFront User Role Editor

WPFront User Role Editor extends the capabilities of user role management in WordPress, offering the ability to manage permissions and menu visibility.

Features:

  • Create, edit, and delete roles.
  • Set custom permissions and restrict menu items.
  • Works well for customizing the dashboard based on user roles.

Best For: Users who need a user-friendly interface for managing roles and hiding admin menu items based on roles.

Each of these plugins provides different features with the hiding feature common in all of them. You can choose any one of them based on your specific needs for managing visibility of items and other customization.

FAQs About Hiding Menu Items Based on Role in WordPress

Will hiding menu items affect user permissions?
No, hiding menu items does not change user permissions. It only alters the visibility of the menu items. Users will still be restricted based on their assigned roles.
Is it possible to hide menu items based on specific user IDs?
Yes, you can use the get_current_user_id() function to retrieve the current user's ID and then apply conditional logic based on that value.
Can I revert the changes made to the menu items?
Yes, you can easily revert the changes by removing the custom code from your functions.php file or deactivating the plugin used to hide the menu items. The menu items will reappear in the admin dashboard.

Final Thoughts

By hiding menu items based on user roles, you can enhance security and user experience on the administrator end. Once you hide the unnecessary menu items, the navigation gets more streamlined with no additional options. There are two methods you can choose from to hide menu items:

  • Choose Using Plugin: If you want a simple and user friendly interface way to hide menu items based on role.
  • Choose Using Custom code: If you are comfortable with coding and want more praise control over the user permission.

For the plugin method there are various alternatives you can go for. On the other hand custom coding is ideal for developers with more expertise. If you want to make your administration more secure with such practices, hire WordPress developers.

Struggling to build a custom and robust WordPress site?

author
Mehul Patel is a seasoned IT Engineer with expertise as a WordPress Developer. With a strong background in Core PHP and WordPress, he has excelled in website development, theme customization, and plugin development.

Leave a comment