WP-Members redirect if member ALREADY logged in

I am using the plugin WP-Members. My WordPress front page is the login page for WP-Members.

In my theme functions.php I have used this WP-Members filter and function to redirect members to the posts page once they have logged in.

Read More
add_filter( 'wpmem_login_redirect', 'my_login_redirect' );

function my_login_redirect()
{
// return the url that the login should redirect to
return 'http://example.com/specials/';
}

And this is working as designed. However, when someone returns to the site and is already logged in from a previous session this redirect does not detect their logged in status and redirect them. I assume because the function is not called until someone logs in.

I am experienced with programming but new to working in WordPress. I have not been able to find any information on how to resolve this, although I strongly suspect it is out there. Can anyone help point me in the right direction?

I appreciate everyone’s help and guidance in advance.

Thank you!

Related posts

1 comment

  1. I assume because the function is not called until someone logs in.

    That is correct. wpmem_login_redirect is a filter that happens when a user is actually logging in. If they are coming to the site and, due to having a valid cookie, are in a logged in state, they will not be redirected.

    From a user experience standpoint, it’s not really good form to just redirect someone when they return, even if that person is in a logged in state. Likewise, you are going to have a very hard time finding a method that doesn’t just redirect them regardless of whether they are returning or browsing the site.

    The function that you want to use for redirection is wp_redirect(). You could attach that to some action that occurs before any info is sent downstream to the browser (such as init) and test to see if they are logged in:


    add_action( 'init', 'my_redirect' );
    function my_redirect(){
    if( is_user_logged_in() ) {
    wp_redirect( 'http://somedomain.com/some-page' );
    exit();
    }
    }

    Like I said, this doesn’t differentiate whether the user has just shown up, or is coming from one page to another, so to make this actually useful and not annoying, you would need to add some kind of process of determining that and acting accordingly.

Comments are closed.