是否有功能可以知道用户是否是WP/woocommerce中的"shop_manager"

mrb*_*ang 4 php wordpress woocommerce

我想知道shop_manager是否已登录WP/woocommerce.我知道函数is_admin(),但你知道一种方法来使用像'is_shop_manager()'这样的东西吗?

谢谢

Anf*_*ipe 8

其实是的,有!

current_user_can( 'manage_woocommerce' );
Run Code Online (Sandbox Code Playgroud)

文档:

current_user_can($能力)

'manage_woocommerce'


Rat*_*pps 4

不,没有任何直接的内置函数,因为 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
}
Run Code Online (Sandbox Code Playgroud)

希望这会有用。