1

I need to run some functions/filters for specific user roles. This code filters the admin order page (Woocommerce) to only show orders that are in GBP.

add_filter('request', function($vars) {
global $typenow, $wpdb;

// Add logic to determine if the orders list should be filtered, and which
// currency
$currency = 'GBP';

if($typenow == 'shop_order') {
$vars['meta_query']['relation'] = 'AND';
// Only show orders in the specific currency
$vars['meta_query'] = array(
    array(
        'key' => '_order_currency',
        'value' => $currency,
        'compare' => '=',
        ),
    );
}
return $vars;
}, 15 );

This works as expected. What I need to do is make this code only run for certain roles. The role I need to fire it on is uk_staff. I then need to create the same thing except for $currency = 'AUD'; and the role au_staff.

Can anyone give me some help making this function only fire for certain roles?

3
  • 3
    You should ask inside your custom function for the role, if ( ! current_user_can( 'manage_options' ) ) return; .
    – bueltge
    Jan 2, 2018 at 8:00
  • @bueltge I'm not sure how to tie that in with the other if statement, can I simply add it before the other if statement?
    – Nik
    Jan 2, 2018 at 8:40
  • You should add my example after the global statement, to that if the user have not (!) the role, exit/return of the request.
    – bueltge
    Jan 2, 2018 at 9:28

1 Answer 1

4

The core has functions like current_user_can to check for an capability of a role or check the role directly. The follow example extend your source for this check so that the function not (!) run, if the current user haven't the rights, the capability of his role.

add_filter('request', function( $vars ) {

// Check the capability of the current user.
if ( ! current_user_can( 'manage_options' ) )
    return;

global $typenow, $wpdb;

// Add logic to determine if the orders list should be filtered, and which
// currency
$currency = 'GBP';

if($typenow == 'shop_order') {
$vars['meta_query']['relation'] = 'AND';
// Only show orders in the specific currency
$vars['meta_query'] = array(
    array(
        'key'     => '_order_currency',
        'value'   => $currency,
        'compare' => '=',
        ),
    );
}
return $vars;
}, 15 );
2
  • Thanks for this. When I try and implement on it's on its own it works as expected. When I try and add another condition it just shows blank, can you check this gist and let me know what I am doing wrong? gist.github.com/webthread/9fd801090f1864185e1485b9ec90e56c
    – Nik
    Jan 4, 2018 at 16:46
  • 1
    Active the WP_DEBUG mode so that you get an error message. This should helps you. Also, you should create a function, there you use only once, with different values for the rights. Currently you run redundant dynamically the same logic, only different capability to check.
    – bueltge
    Jan 5, 2018 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.