【发布时间】:2013-07-16 10:22:19
【问题描述】:
我想知道 shop_manager 是否已登录 WP/woocommerce。我知道函数 is_admin(),但你知道使用类似 'is_shop_manager()' 的方法吗?
谢谢
【问题讨论】:
标签: php wordpress woocommerce
我想知道 shop_manager 是否已登录 WP/woocommerce。我知道函数 is_admin(),但你知道使用类似 'is_shop_manager()' 的方法吗?
谢谢
【问题讨论】:
标签: php wordpress woocommerce
其实是的,有!
current_user_can( 'manage_woocommerce' );
文档:
【讨论】:
固定代码:
function is_shop_manager() {
$user = wp_get_current_user();
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
return true; // when user is shop manager
} else {
return false; // when user is not shop manager
}
}
【讨论】:
不,没有任何直接的内置功能,因为 shop_manager 角色来自 WooCommerce 而不是来自 WordPress,但可以通过以下代码实现:
function is_shop_manager() {
$user = wp_get_current_user();
if ( isset( $user['roles'][0] ) && $user['roles'][0] == 'shop_manager' ) {
return true; // when user is shop manager
} else {
return false; // when user is not shop manager
}
}
if ( is_shop_manager() ) {
// write code for shop_manager here
}
希望这将是有用的。
【讨论】: