woocommerce offtopic - Remove Bulk Action For non admin user

admin2025-06-04  3

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.

Share Improve this question edited Jan 25, 2019 at 0:26 Kashif Rafique 2351 gold badge3 silver badges12 bronze badges asked Jan 24, 2019 at 22:29 Houssem GhozziHoussem Ghozzi 32 bronze badges 1
  • Out of curiosity, why? – Tom J Nowell Commented Jan 25, 2019 at 0:27
Add a comment  | 

2 Answers 2

Reset to default 1

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' ) ) { ...
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748976171a315313.html

最新回复(0)