function pmpro_login_redirect($redirect_to, $request, $user)
{
    global $wpdb;
    //is a user logging in?
    if (!empty($user->ID)) {
        //logging in, let's figure out where to send them
        if (pmpro_isAdmin($user->ID)) {
            //admins go to dashboard
            $redirect_to = get_bloginfo("url") . "/wp-admin/";
        } elseif (strpos($redirect_to, "checkout") !== false) {
            //if the redirect url includes the word checkout, leave it alone
        } elseif ($wpdb->get_var("SELECT membership_id FROM {$wpdb->pmpro_memberships_users} WHERE status = 'active' AND user_id = '" . $user->ID . "' LIMIT 1")) {
            //if logged in and a member, send to wherever they were going
        } else {
            //not a member, send to subscription page
            $redirect_to = pmpro_url("levels");
        }
    } else {
        //not logging in (login form) so return what was given
    }
    //let's strip the https if force_ssl_login is set, but force_ssl_admin is not
    if (force_ssl_login() && !force_ssl_admin()) {
        $redirect_to = str_replace("https:", "http:", $redirect_to);
    }
    return apply_filters("pmpro_login_redirect_url", $redirect_to, $request, $user);
}
        } else {
            //uh oh. we charged them then the membership creation failed
            if (isset($morder) && $morder->cancel()) {
                $pmpro_msg = __("IMPORTANT: Something went wrong during membership creation. Your credit card authorized, but we cancelled the order immediately. You should not try to submit this form again. Please contact the site owner to fix this issue.", "pmpro");
                $morder = NULL;
            } else {
                $pmpro_msg = __("IMPORTANT: Something went wrong during membership creation. Your credit card was charged, but we couldn't assign your membership. You should not submit this form again. Please contact the site owner to fix this issue.", "pmpro");
            }
        }
    }
}
//default values
if (empty($submit)) {
    //show message if the payment gateway is not setup yet
    if ($pmpro_requirebilling && !pmpro_getOption("gateway", true)) {
        if (pmpro_isAdmin()) {
            $pmpro_msg = sprintf(__('You must <a href="%s">set up a Payment Gateway</a> before any payments will be processed.', 'pmpro'), get_admin_url(NULL, '/admin.php?page=pmpro-membershiplevels&view=payment'));
        } else {
            $pmpro_msg = __("A Payment Gateway must be set up before any payments will be processed.", "pmpro");
        }
        $pmpro_msgt = "";
    }
    //default values from DB
    if (!empty($current_user->ID)) {
        $bfirstname = get_user_meta($current_user->ID, "pmpro_bfirstname", true);
        $blastname = get_user_meta($current_user->ID, "pmpro_blastname", true);
        $baddress1 = get_user_meta($current_user->ID, "pmpro_baddress1", true);
        $baddress2 = get_user_meta($current_user->ID, "pmpro_baddress2", true);
        $bcity = get_user_meta($current_user->ID, "pmpro_bcity", true);
        $bstate = get_user_meta($current_user->ID, "pmpro_bstate", true);
        $bzipcode = get_user_meta($current_user->ID, "pmpro_bzipcode", true);