PHP有条件检查多个WordPress角色

发布于 2025-02-08 17:36:00 字数 363 浏览 3 评论 0原文

我正在寻找更多的性能代码来选择多个WordPress角色作为检查来运行一些代码的检查:

 if ( current_user_can( 'wholesale_customer' ) || current_user_can( 'wholesale_premium' ) || current_user_can( 'wholesale_nz') || current_user_can( 'wholesale_wa') ){
            // do something
     }

上述代码有效,但我疑问是最佳的,据我了解,您无法将多个角色传递到 current_user_user_can 作为一个数组,但更多的功能。

I am looking for more performant code for selecting multiple WordPress roles as a check to run some code:

 if ( current_user_can( 'wholesale_customer' ) || current_user_can( 'wholesale_premium' ) || current_user_can( 'wholesale_nz') || current_user_can( 'wholesale_wa') ){
            // do something
     }

The above code works but I question is it optimal, from what I understand you can't pass multiple roles into current_user_can as an array but more so capabilities.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

A君 2025-02-15 17:36:00

您也可以使用user_roles_array检查 -

$user_roles = array( 'wholesale_customer', 'wholesale_premium', 'wholesale_nz', 'wholesale_wa' );
foreach( $user_roles as $role ){
    if( current_user_can( $role ) ){
        // do something
    }
}

You can also check with user_roles_array like this -

$user_roles = array( 'wholesale_customer', 'wholesale_premium', 'wholesale_nz', 'wholesale_wa' );
foreach( $user_roles as $role ){
    if( current_user_can( $role ) ){
        // do something
    }
}
热风软妹 2025-02-15 17:36:00

那一个 -

$current_user = wp_get_current_user();
$user_roles = $current_user->roles;
$compare_user_roles = array( 'wholesale_customer', 'wholesale_premium', 'wholesale_nz', 'wholesale_wa' );
foreach( $user_roles as $user_role ){
    if( in_array( $user_role, $compare_user_roles ) ){
        // echo $user_role;
    }
}

How about this one -

$current_user = wp_get_current_user();
$user_roles = $current_user->roles;
$compare_user_roles = array( 'wholesale_customer', 'wholesale_premium', 'wholesale_nz', 'wholesale_wa' );
foreach( $user_roles as $user_role ){
    if( in_array( $user_role, $compare_user_roles ) ){
        // echo $user_role;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文