php - 检查 WooCommerce 中 Hook 功能破坏网站上的用户角色

标签 php wordpress woocommerce account user-roles

如果存在特定用户,我正在尝试向“我的帐户”页面添加一个选项卡。
我创建了一个 check_user_role 函数来检查特定角色是否存在。
如果存在 premium_member 角色,则应在“我的帐户”页面中添加一个额外的选项卡。
现在,当我添加 check_user_role 函数时,它会破坏我的网站。 一切都在 functions.php 文件中

function check_user_role($roles, $user_id = null) {
  if ($user_id) $user = get_userdata($user_id);
  else $user = wp_get_current_user();
  if (empty($user)) return false;
  foreach ($user->roles as $role) {
    if (in_array($role, $roles)) {
      return true;
    }
  }
  return false;
}

// ------------------
// 1. Register new endpoint (URL) for My Account page
// Note: Re-save Permalinks or it will give 404 error
  
function vehicle_intake_form_endpoint() {
    add_rewrite_endpoint( 'vehicle-intake-form', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'vehicle_intake_form_endpoint' );
  
// ------------------
// 2. Add new query var
  
function vehicle_intake_form_query_vars( $vars ) {
    $vars[] = 'vehicle-intake-form';
    return $vars;
}
  
add_filter( 'query_vars', 'vehicle_intake_form_query_vars', 0 );
  
// ------------------
// 3. Insert the new endpoint into the My Account menu
if(check_user_role(array('premium_member'))) {
    function vehicle_intake_form_link_my_account( $items ) {
        $items['vehicle-intake-form'] = 'Vehicle Intake Form';
        return $items;

    }
    add_filter( 'woocommerce_account_menu_items', 'vehicle_intake_form_link_my_account' );
}
// ------------------
// 4. Add content to the new tab
  
function vehicle_intake_form_content() {
   echo '<h3>Premium WooCommerce Support</h3><p>Welcome to the WooCommerce support area. As a premium customer, you can submit a ticket should you have any WooCommerce issues with your website, snippets or customization. <i>Please contact your theme/plugin developer for theme/plugin-related support.</i></p>';
   //echo do_shortcode( ' /* your shortcode here */ ' );
}
  
add_action( 'woocommerce_account_vehicle-intake-form_endpoint', 'vehicle_intake_form_content' );

最佳答案

您的 If 语句需要位于函数内部,而不是外部。

您可以使用current_user_can() function ,但是你的 Hook 函数中,例如:

add_filter( 'woocommerce_account_menu_items', 'vehicle_intake_form_link_my_account' );
function vehicle_intake_form_link_my_account( $items ) {
    if( current_user_can('premium_member') ) {
        $items['vehicle-intake-form'] = __('Vehicle Intake Form');
    }
    return $items;
}

或者使用 check_user_role() 重新访问的函数在您的 Hook 函数中使用,例如:

function check_user_role( $roles, $user_id = null ) {
    $user = $user_id ? new WP_User( $user_id ) : wp_get_current_user();

    if ( is_a($user, 'WP_User') && count( array_intersect($roles, $user->roles) ) > 0 ) {
        return true;
    }
    return false;
}

add_filter( 'woocommerce_account_menu_items', 'vehicle_intake_form_link_my_account' );
function vehicle_intake_form_link_my_account( $items ) {
    if( check_user_role( array('premium_member') ) ) {
        $items['vehicle-intake-form'] = __('Vehicle Intake Form');
    }
    return $items;
}

代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效。

关于php - 检查 WooCommerce 中 Hook 功能破坏网站上的用户角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66990174/

相关文章:

php - 具有 PSR-4 自动加载功能的 Composer : classes from namespace not loading

php - 无法自动增加 session 值

javascript - 在谷歌地图的 HTML/JavaScript var 中使用 PHP 回显的结果

wordpress - 如何为小部件提供单独的 ID 或类?

php - 如何删除 woocommerce 选项卡?

php - 在 WooCommerce 我的账户订单列表中显示产品缩略图

php - Laravel 路由变量限制为 17 个字符

php - WooCommerce 产品 : Allow backorders only for specific user roles

wordpress - woocommerce 信用卡付款有条件折扣

php - WooCommerce 购物车 - 动态价格变量传递到自定义价格 Hook