How to hide the administrator bar according to the woocommerce customer or user role

If the user is not present, an empty array is returned.
}
} functions. When adding to PHP and testing this code, the current user role must be displayed in the array, as follows:
You can use male key [0] to obtain the user role from the array. As long as you slightly modify the code and print the user role, you can see that the printed user role is the same as that in the next modified code. add_action( ‘wp_head’, ‘njengah_get_current_user_role_print’);
Function njengah_get_current_user_role_print(){
If (is_user_logged_in() {\/ \/ confirm that there is a logged in user
$user = wp_get_current_user(); \/\/ Import and set current user
$roles = (array) $user – > roles\/\/ Get role
Eco $role [0];
}Another{
Return array ()\/\/ If there is no logged in user, an empty array is returned.
}
}Since the user role has been successfully obtained, the administrator column that can be used as a specific user role for all roles can now be conditionally hidden in the show_admin_bar() function.
For illustration, now use the following code to hide the administrator column of the administrator role. add_action( ‘wp_head’, ‘njengah_hide_admin_bar_for_administrator’);
Function njengah_hide_admin_bar_for_administrator (){
If (is_user_logged_in() {\/ \/ confirm that there is a logged in user
$user = wp_get_current_user(); \/\/ Import and set current user
$roles = (array) $user – > roles\/\/ Get role
If ($roles [0] = = \
Eco $role [0]\/\/ For demonstration purposes, print the role on the screen (it must be removed from production).
Show_admin_bar (false)\/\/ Hide the role of this condition.
}
}Another{
Return array ()\/\/ If there is no logged in user, an empty array is returned.
}
}Function. After adding it to the PHP file, you will see that the administrator role is displayed on the screen and the administrator bar is conditionally hidden.
You can replace this administrator role with a customer and add multiple conditional tests to the following conditions: add_action( ‘wp_head’, ‘njengah_hide_admin_bar_for_customer’);
Function njengah_hide_admin_bar_for_administrator (){
If (is_user_logged_in() {\/ \/ confirm that there is a logged in user
$user = wp_get_current_user(); \/\/ Import and set current user
$roles = (array) $user – > roles\/\/ Get role
If ($roles [0] = = \
Eco $role [0]\/\/ For demonstration purposes, print the role on the screen (it must be removed from production).
Show_admin_bar (false)\/\/ tooth
Hide the role of the condition.
}
}Another{
Return array ()\/\/ If there is no logged in user, an empty array is returned.
}
}As in the administrator test, the role name should be displayed as customer and the administrator column should be hidden.
Another way to hide the management bar using current user functions is functions to prevent all users from expecting management. You can add the following code to PHP: if ( ! current_user_can( ‘manage_options’ ) ) {
Show_admin_bar (false);
}This code uses current_user_can() conditional test. You can edit the customer with the following different permissions: If (! Current_user_can){
Show_admin_bar (false);
}This is the same role as the word subscriber. Woocomerce creates two roles: customer and shop manager. The customer is the same as the subscriber, so in this case, the subscriber’s function is \
You can view all the functions of WordPress users in the role and function document. In the woocommerce role and function document, you can learn more about the woocommerce role. In the code above, we have described in detail all the operations of hiding the administrator bar in woocommerce. For testing purposes, I used add_action () attached to \
In this case, the filter must be connected to \
Function njengah_hide_admin_bar_for_customers ($show){
$user = wp_get_current_user(); \/\/ Import and set current user
$roles = (array) $user – > roles\/\/ Get role
If ($roles [0] = = \
$show = false;
Return $show;
}
}You can also use the function of each role to hide the administrator bar. The filter is:\/**
*Hide the administrator bar for other roles
*\/
add_filter( ‘show_admin_bar’, ‘njengah_hide_admin_bar_others_expect_admin’, 20, 1 );
Function njengah_hide_admin_bar_others_expect_admin ($show){
if ( ! current_user_can( ‘administrator’ ) ) $show = false;
Return $show;
}Function. Add to PHP and hide the administrator bar in other roles expected by the administrator. The last idea describes how to use multiple methods to hide the administrator bar in word press or woocomer. Select one of the methods, functions. You can add code to PHP. You can also further modify the code to suit various situations, such as hiding the administrator bar in WordPress custom user roles.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *