I want to remove bulk action for non admin user (shop manager to be specific), so I got this code that let me to remove the bulk action for all users.
add_filter( 'bulk_actions-edit-shop_order', '__return_empty_array', 100 );
How can I make it working only for the shop manager role users?
I also tried this, but it didn't work.
Thanks.
I want to remove bulk action for non admin user (shop manager to be specific), so I got this code that let me to remove the bulk action for all users.
add_filter( 'bulk_actions-edit-shop_order', '__return_empty_array', 100 );
How can I make it working only for the shop manager role users?
I also tried this, but it didn't work.
Thanks.
Here's how you can remove bulk actions for any user not an administrator.
add_action( 'wp_loaded', 'my_remove_bulk_actions' );
function my_remove_bulk_actions() {
if ( ! is_admin() )
return;
if ( ! current_user_can( 'administrator' ) ) {
add_filter( 'bulk_actions-edit-shop_order', '__return_empty_array', 100 );
}
}
If you would like to target just the shop manager you can edit the if statement
if ( ! current_user_can( 'administrator' ) )
to
if ( current_user_can( 'shop_manager' ) )
Tested and working on latest version of WordPress, 5.0.3 at time of posting.
As per Codex:
While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.
It further stated:
Passing role names to
current_user_can()
is discouraged as this is not guaranteed to work correctly (see #22624).
Therefore, current_user_can()
should not be used to check a user's role. It should be used to check if a user has a specific capability.
You may change the Shawn w code as follows:
// for all the non-admin roles
if ( ! current_user_can( 'manage_options' ) ) { ...
// just for the Shope Manager role
if ( ! current_user_can( 'manage_options' ) && current_user_can( 'manage_woocommerce' ) ) { ...