【问题标题】:Is there a function for knowing if user is 'shop_manager' in WP / woocommerce是否有知道用户是否是 WP / woocommerce 中的“shop_manager”的功能
【发布时间】:2013-07-16 10:22:19
【问题描述】:

我想知道 shop_manager 是否已登录 WP/woocommerce。我知道函数 is_admin(),但你知道使用类似 'is_shop_manager()' 的方法吗?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    其实是的,有!

    current_user_can( 'manage_woocommerce' );
    

    文档:

    current_user_can($capability)

    'manage_woocommerce'

    【讨论】:

      【解决方案2】:

      固定代码:

      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
          }
      }
      

      【讨论】:

        【解决方案3】:

        不,没有任何直接的内置功能,因为 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
        }
        

        希望这将是有用的。

        【讨论】:

        • 确实有效,但我认为@Anfelipe 的回答更简洁。
        猜你喜欢
        • 1970-01-01
        • 2016-08-19
        • 2020-06-30
        • 1970-01-01
        • 2011-02-06
        • 2019-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多