Limit Access
By default, only users with administrator privileges can access the export feature. However, real‑world stores often need to delegate this task to shop managers, accounting staff, or external assistants without granting full administrative rights.
Restricting Access to Administrators Only
The example demonstrates how to enforce that only users with the administrator role can access the export feature.
|
1 2 3 4 5 |
// only Admins will see >WooCommerce>Export Orders add_filter("woe_current_user_can_export", function($can) { $user = wp_get_current_user(); return in_array( 'administrator', (array) $user->roles ) ; }); |
What this code does:
- Attaches an anonymous function to the
woe_current_user_can_exportfilter. - Retrieves the current user object using
wp_get_current_user(). - Checks whether the user’s roles array contains the
administratorrole. - Returns
trueif the user is an administrator, otherwisefalse.
This is functionally identical to the default behaviour. However, it demonstrates how you can replace the default capability check with a role‑based condition.