-1

The problem is not the first redirection (already solved in other case here), the problem is the second redirection back.

No problem with the first redirection, when no logged in user come to page ID 10 (checkout page) he is directly redirect to custom login page but when he log in he is not redirect back to the checkout page, instead we can see before the login process in the url xxx.com/myaccount/?redirect_to=xxx.com/checkout/, he stay in the account page and dont go back automatically to the checkout page.

This is the code I used:

add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
    $pageid = 10; // checkout page
    if(!is_user_logged_in() && is_page($pageid))
    {
        $url = add_query_arg(
            'redirect_to',
            get_permalink($pagid),
            site_url('/myaccount/')
        );
        wp_redirect($url);
        exit;
    }
}

I just need that the second back redirection to the checkout page works. This is what we need : No logged in user -> checkout page -> login page -> logged in -> checkout page

Any help would be appreciated. Thanks.

  • myaccount is not the standard WordPress login page. It is a custom one? Is so, how do you handle the user log in? – cybmeta Jan 11 '16 at 7:16
  • No it is not the standart wordpress page. It is the standart woocommerce login page. – Erdo Jan 11 '16 at 13:14
  • 1
    In that case you should ask to WooCommerce support team. Third party plugins are off-topic here as we may not know how it works. – cybmeta Jan 11 '16 at 14:01
  • I already did it. Of course you may not know how it works. Just hope someone knows. – Erdo Jan 11 '16 at 15:12
  • Sorry but "if someone knows" the answer, it doesn't make the question be on topic. Please, read the help about what topics you can ask here. – cybmeta Jan 11 '16 at 16:12
0

You need to modify the login form on your custom login page. When using wp_login_form() you can include a redirect URL in the arguments (Codex Entry).

The default URL is:

( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']

But you can override that:

if (get_query_var('redirect_to')) {
    $redirectURL = get_query_var('redirect_to');
    wp_login_form(array('redirect' => $redirectURL));
} else {
    wp_login_form();
}
  • Mike, read the comments. The login form the OP is using is the login from generated by WooCommerce. – cybmeta Jan 14 '16 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.