$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - Remove Bulk Action For non admin user|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - Remove Bulk Action For non admin user

matteradmin9PV0评论

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' ) ) { ...
Post a comment

comment list (0)

  1. No comments so far