This is How to Show the WordPress Admin Bar on the Frontend for Specific User Roles
As a WordPress developer or site admin, you may have encountered a scenario where you need to display the WordPress admin bar on the frontend for certain user roles, while hiding it for others. This can be particularly useful when you want to provide easy access to the admin dashboard for specific users, such as editors or shop managers, without overwhelming regular website visitors.
In this article, we'll dive into a simple yet effective solution to this problem, using a code snippet that you can easily implement on your WordPress site.
Understanding the Problem
By default, the WordPress admin bar is visible to all logged-in users, regardless of their roles or permissions. This can be a problem if you want to maintain a clean and uncluttered frontend experience for certain user groups, while still allowing specific roles to access the admin tools and functionality.
Hiding the admin bar completely may not be an ideal solution, as it can hinder the workflow and productivity of users who need access to the admin features. Instead, a more targeted approach is to selectively display the admin bar based on the user's role.
The Solution: A Custom Function to Control Admin Bar Visibility
To achieve this, we'll create a custom function that checks the current user's roles and then decides whether to show or hide the admin bar. Here's the code snippet:
function admin_bar() {
if (is_user_logged_in()) {
$user = wp_get_current_user();
if (in_array('shop_manager', (array) $user->roles) || in_array('editor', (array) $user->roles)) {
add_filter('show_admin_bar', '__return_true', 1000);
} else {
add_filter('show_admin_bar', '__return_false', 1000);
}
}
}
add_action('init', 'admin_bar');
Let's break down how this code works:
- The
admin_bar()
function is defined, which will be hooked into the init
action.
- Inside the function, we first check if the current user is logged in using the
is_user_logged_in()
function.
- If the user is logged in, we retrieve the current user's roles using the
wp_get_current_user()
function and store them in the $user
variable.
- We then use the
in_array()
function to check if the user's roles include either "shop_manager" or "editor".
- If the user's roles include "shop_manager" or "editor", we add a filter to the
show_admin_bar
hook, setting it to true
with a priority of 1000. This will display the admin bar on the frontend for these user roles.
- If the user's roles do not include "shop_manager" or "editor", we add a filter to the
show_admin_bar
hook, setting it to false
with a priority of 1000. This will hide the admin bar on the frontend for these user roles.
By using the add_action('init', 'admin_bar')
line, we ensure that the admin_bar()
function is executed on the init
action, which is triggered early in the WordPress loading process. This allows us to control the visibility of the admin bar before it is rendered on the page.
Real-World Example and Benefits
Let's consider a real-world scenario where this solution can be beneficial:
Imagine you're running an e-commerce website built on WordPress, and you have two types of user roles: "shop_manager" and "customer". The shop managers need access to the admin tools to manage products, orders, and other store-related functionality, while the regular customers should not see the admin bar, as it can be distracting and confusing.
By implementing the code snippet above, you can ensure that the shop managers (who have the "shop_manager" role) can see the admin bar on the frontend, allowing them to access the necessary admin features and tools. At the same time, the regular customers (who do not have the "shop_manager" or "editor" role) will not see the admin bar, maintaining a clean and focused user experience.
The key benefits of this solution include:
-
Improved User Experience: By selectively displaying the admin bar, you can create a more streamlined and focused frontend experience for your website's users, reducing clutter and potential confusion.
-
Enhanced Productivity: Providing easy access to the admin tools for specific user roles, such as shop managers and editors, can significantly improve their workflow and productivity, as they can quickly access the necessary features and functionality.
-
Better Security: By hiding the admin bar from users who don't need access to the backend, you're reducing the potential attack surface and minimizing the risk of unauthorized access to the admin area.
-
Flexibility: This solution is easily adaptable to your specific needs. You can easily modify the user roles or add additional roles as required, without having to change the core functionality of the code.
Conclusion
In this article, we've provided a straightforward solution to the problem of selectively showing the WordPress admin bar on the frontend for specific user roles. By implementing the code snippet we've discussed, you can now easily control the visibility of the admin bar, ensuring that your website's users have an optimal and tailored experience.
Remember, the key to a successful WordPress site is creating a balance between functionality and usability. By leveraging this technique, you can strike that balance and provide your users with a seamless and efficient experience.
If you have any further questions or need assistance with implementing this solution, feel free to reach out to the team at Flowpoint.ai. We're always happy to help WordPress developers and site owners optimize their websites and improve user engagement.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.