Support » Plugin: Force Login » Redirect filter does not work when logging in after logout

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Kevin Vess

    (@kevinvess)

    Hi, great question – try adding this code below your v_forcelogin_redirect filter:

    add_filter('login_redirect', 'my_forcelogin_redirect', 10, 3);

    Thanks, Kevin. That took care of it, but I realize now redirecting every user to the same page upon login is not my desired functionality.

    My end goal is to create a strictly front-end experience and keep users out of WP admin by either redirecting to the page they requested or redirecting to the home page if they access the login page directly. Here are the scenarios:

    • If unauthorized user attempts to visit a page, they are forced to log in and redirected back to the page they originally requested (default Force Login behavior).
    • If unauthorized user attempts to log in directly from wp-login.php with no redirect_to query parameter, they are redirected to the home page upon login.
    • If user just logged out and logs back in, they are redirected to home page.

    So I guess I’m looking at default Force Login behavior with the addition of a check for redirect_to query parameter. Let me know if this is outside the scope of your plugin but any help is appreciated.

    Plugin Author Kevin Vess

    (@kevinvess)

    Try this instead; remove the v_forcelogin_redirect filter as described in the FAQ and use the following code instead:

    /**
     * Set the URL to redirect to on login.
     *
     * @return string URL to redirect to on login. Must be absolute.
     **/
    function my_login_redirect($redirect_to, $request, $user) {
      if( !isset($_REQUEST['redirect_to']) )
        return home_url();
      else
        return $redirect_to;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3);
    fractals

    (@fractals)

    just got your plugin today to protect a site I have built for my daughter and grandaughter – it is great – thank you – but I am an amateur.

    I got the login working – but when you login – I want to just to go to the home page – not into the admin section

    I found this

    /**
    * Set the URL to redirect to on login.
    *
    * @return string URL to redirect to on login. Must be absolute.
    **/
    function my_forcelogin_redirect() {
    return site_url( ‘/mypage/’ );
    }
    add_filter(‘v_forcelogin_redirect’, ‘my_forcelogin_redirect’, 10, 1);

    and have tried every version- but not being a coder – I am obviously got getting it right

    what would that code look like for domain = http://www.foo.com and directory = /test

    at one point I even managed to kill my site completely through incorrectly applying the URLs

    many thanks in anticipation

    Plugin Author Kevin Vess

    (@kevinvess)

    when you login – I want to just to go to the home page – not into the admin section

    Try adding only this code to your theme’s functions.php file:

    /**
     * Set the URL to redirect to on login.
     *
     * @return string URL to redirect to on login. Must be absolute.
     **/
    function my_login_redirect($redirect_to, $request, $user) {
      if( !isset($_REQUEST['redirect_to']) )
        return home_url();
      else
        return $redirect_to;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3);

    what would that code look like for domain = http://www.foo.com and directory = /test

    The code example you pasted should have worked; did you add it to your theme’s functions.php file?

    /**
     * Set the URL to redirect to on login.
     *
     * @return string URL to redirect to on login. Must be absolute.
     **/
    function my_forcelogin_redirect() {
      return site_url( '/test/' );
    }
    add_filter('v_forcelogin_redirect', 'my_forcelogin_redirect', 10, 1);

    at one point I even managed to kill my site completely through incorrectly applying the URLs

    Try enabling the WP_DEBUG mode in your site’s wp-config.php file to see what error you get to determine what went wrong.
    https://codex.wordpress.org/WP_DEBUG

    fractals

    (@fractals)

    MANY THANKS.

    I added that new code to functions php – and worked like a charm. And to answer your other question … yes … always added to functions.php

    No need for debug – using Transmit – I just copied my functions.php back up file over the corrupt one that I had mangled – and I was back to success.

    Again – mahalo for help. (And my daughter will be ecstatic !)

    fractals

    (@fractals)

    ok – back again – because I am not getting consistent behavior – and have had others try aswell .. wondering aloud

    I use the domain foo.bar.com – which actually is a redirect from bar.com/foo – should that work consistently , or should I only use bar.com/foo ?

    Plugin Author Kevin Vess

    (@kevinvess)

    I am not getting consistent behavior

    What is happening?

    I use the domain foo.bar.com – which actually is a redirect from bar.com/foo

    This could be causing your problems. I’m not sure what to suggest though.

    Does everything work properly when you use the site with the bar.com/foo URL? If so, then sure – use that URL.

    If you need the other URL, why aren’t you just using foo.bar.com as the Site URL when you install WordPress? Is this a WordPress Multisite install?

    Plugin Author Kevin Vess

    (@kevinvess)

    I should note: the plugin, and those examples for redirects after login above, are all using references to whatever URL you entered as your Site URL as defined in General Settings.

    <?php site_url( $path, $scheme ); ?>
    https://codex.wordpress.org/Function_Reference/site_url

    <?php home_url( $path, $scheme ); ?>
    https://codex.wordpress.org/Function_Reference/home_url

    fractals

    (@fractals)

    brilliant – thank you – I changed the general settings to reflect foo.bar.com – and DONE, all working.

    /J

    Hi,
    I can’t get the redirect to work. I am using a custom login page. Could that be the reason?
    Thanks!

    Plugin Author Kevin Vess

    (@kevinvess)

    @dommusic
    Please keep your posts in one place for easy reference, see my response here:

    Link in reset password email blocked

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Redirect filter does not work when logging in after logout’ is closed to new replies.