我需要为特定的用户角色运行一些函数/过滤器。此代码过滤管理订单页面(Woocommerce),以仅显示以英镑为单位的订单。
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 );
这正如预期的那样。我需要做的是使这段代码只针对某些角色运行。我需要扮演的角色是
uk_staff
.然后我需要创建相同的东西,除了
$currency = \'AUD\';
以及角色
au_staff
.
有谁能帮我让这个功能只针对某些角色启动吗?
最合适的回答,由SO网友:bueltge 整理而成
核心具有以下功能current_user_can
检查角色的功能或直接检查角色。下面的示例扩展此检查的源代码,以便函数(!
) 如果当前用户没有权限,请运行其角色的功能。
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 );