fbpx

If Menu is a great plugin for showing or hiding menu items depending on whether a user is logged in or not, or by role.

The one drawback is that it doesn’t work with custom roles. Here’s the fix:

add_filter( 'if_menu_conditions', 'custom_if_menu_conditions' );
function custom_if_menu_conditions( $conditions ) {
	global $wpdb;
	$value = get_option("{$wpdb->prefix}user_roles");
    foreach ($value as $role_name => $role_info){

        $conditions[] = array(
            'name'      =>  $role_info['name'],
            'condition' =>  function() {
                global $current_user;
                if( is_user_logged_in() ) return in_array( $role_name, $current_user->roles );
                    return false;
                }
        );
    }

	return $conditions;

}