Wordpress Woo Login Redirect coding

Discussion in 'Wordpress Support' started by mjclob, Jul 24, 2019.

  1. mjclob

    mjclob UKBF Contributor Free Member

    71 16
    Hope someone can help. I have a woocommerce site and have the below code to force users to login before they enter the shop pages and redirect them to the previous page

    //Force Login to Woo//
    function wdo_user_redirect() {
    if (! is_user_logged_in() && (is_woocommerce() || is_cart() || is_checkout())) {
    wp_redirect( 'https://www.myurl.co.uk/login' );
    add_action('template_redirect', 'wdo_user_redirect');
    //Force Login to Woo end//

    //Previous page after login//
    // start global session for saving the referer url
    function start_session(){

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

    //login redirect to referer url
    function login_redirect(){

    /** end here */

    I am sending out offers via email with a targeted landing page (woo product page). Only problem, is that if they are not logged in it boots them to the login page but there is no previous page as it doesn't recognise the landing url and just goes to a white screen. Is there a different code for this? I need it to work both ways though as they may be on the site before logging in too.

    Sure that doesn't make sense but any help appreciated
    Posted: Jul 24, 2019 By: mjclob Member since: Apr 3, 2016
  2. fisicx

    fisicx It's Major Clanger! Staff Member

    32,348 9,570
    That's a pretty awful bit of code! It's telling WP to send them to the homepage after login. You need to incorporate this filter:


    But you also need to check if it's an email link or someone trying to view a page. So there is a lot more code needs writing to get it all to work. and you can get rid of all those session variables - they aren't necessary.
    Posted: Jul 24, 2019 By: fisicx Member since: Sep 12, 2006
  3. David-Max

    David-Max UKBF Newcomer Free Member

    18 1
    Adding a simple function to the functions.php file:

    $redirect_to = 'your_url/shop';
    return $redirect_to;

    If you want to set the redirect URL to your homepage use this code:
    $redirect_to = home_url();

    You can set a custom page as well, just change “70” in the example below to your page ID:
    $redirect_to = get_permalink( 70 );

    Or you can also use the free plugin WooCommerce Login Redirect ;)
    Posted: Oct 18, 2019 By: David-Max Member since: Oct 16, 2019