Hide an account menu item

Sometimes you may want to hide an item from the account menu. In version 1.0.3 I added a filter to make this possible.

Hide a menu item for all users

Add the following code to your theme's functions.php file.

/**
 * Hide an account menu item.
 * 
 * @param array $hidden
 *
 * @return array
 */
function iconic_hidden_pages( $hidden ) {
	$hidden[] = 'example-page';

	return $hidden;
}

add_filter( 'iconic_wap_hidden_pages', 'iconic_hidden_pages', 10, 1 );

The filter accepts 1 parameter:

  • $hidden
    This is an array of page slugs. In this example we're adding example-page as a hidden item.

This page will now be hidden from the menu. You can add more pages by duplicating this line $hidden[] = 'example-page';.

Hide a menu item for a specific user role

If you want to hide a menu item from a specific user role, you can do the following:

/**
 * Hide an account menu item.
 *
 * @param array $hidden
 *
 * @return array
 */
function iconic_hidden_pages( $hidden ) {
	if ( iconic_get_current_user_role() === 'wholesale' ) {
		$hidden[] = 'example-page';
	}

	return $hidden;
}

add_filter( 'iconic_wap_hidden_pages', 'iconic_hidden_pages', 10, 1 );

/**
 * Get current user role.
 * 
 * @return bool|string
 */
function iconic_get_current_user_role() {
	if( is_user_logged_in() ) {
		$user = wp_get_current_user();
		$role = ( array ) $user->roles;
		return $role[0];
	} else {
		return false;
	}
}

The code is much the same as before, except this time we're using iconic_get_current_user_role() to determine if the user has the wholesale role. If they do, we'll hide the example-page menu item.