Grzes Slania
Grzes Slania

Reputation: 602

Checking user role on a hooked function breaking website in WooCommerce

I am trying to add a tab to the My Account page if a specific user exists.
I created a check_user_role function that checks if a specific role exists.
If the premium_member role exists an extra tab should be added to the My Account page.
Right now when I add the check_user_role function it breaks my website. Everything is in the functions.php file

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' );

Upvotes: 2

Views: 380

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254182

Your If statement needs to be inside the function, never outside it.

You can use current_user_can() function, but inside your hooked function like:

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

Or use check_user_role() revisited function to be used inside your hooked function like:

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

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Upvotes: 2

Related Questions