简体   繁体   中英

I added code to divert users from the WP-login page to the Woocomerce login page in functions.php file but now I am getting a blank screen after login

I am building a WordPress website using the OceanWP theme and WP-job-manager and woocommerce plugins.

As many WP-job-manager links divert users to the default Wordpress login page, I wanted all users to login using the woocommerce login page instead so I added the following code to my functions.php file:

/**
* direct users to woocommerce login page
*/

add_filter( 'login_url', 'my_login_page', 10, 2 );
function my_login_page( $login_url, $redirect ) {
    return home_url( '/my-account/?redirect_to=' . $redirect );
}

/** Log in redirect to previous page by logicdigger **/
// start global session for saving the referer url
function start_session() {
    if(!session_id()) {
        session_start();
    }
}
add_action('init', 'start_session', 1);

// get the referer url and save it to the session
function redirect_url() {
    if (! is_user_logged_in()) {
        $_SESSION['referer_url'] = wp_get_referer();
    } else {
        session_destroy();
    }
}
add_action( 'template_redirect', 'redirect_url' );

//login redirect to referer url
function login_redirect() {
    if (isset($_SESSION['referer_url'])) {
        wp_redirect($_SESSION['referer_url']);
    } else {
        wp_redirect(home_url());
    }
}
add_filter('woocommerce_login_redirect', 'login_redirect', 1100, 2);

It seems to work well and diverts all users to the woocommerce login page but the problem I have is that when a user logs in using the woocommerce login page they get a blank white screen, even though the URL is "www.mywebsite.com/my-account" which is the correct URL for the my-account page.

...but when I refresh the page, the page redirects to the page the user was on previously, which is how I wanted it to work.

I was just wondering what might be causing the blank white screen after login?

I have cleared my cache and retried but the same thing happens. I really don't know what is causing it.

Many Thanks in advance.

...but when I refresh the page, the page redirects to the page the user was on previously, which is how I wanted it to work.

I tested your snippet and I get the same.

Question: Once you are using the WooCommerce login form, why use the rest of the functions to handle sessions? I reckon WooCommerce does it for you so I am thinking using:

/**
* direct users to woocommerce login page
*/

add_filter( 'login_url', 'my_login_page', 10, 2 );
function my_login_page( $login_url, $redirect ) {
    return home_url( '/my-account/?redirect_to=' . $redirect );
}

should suffice, no(?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM