[Support request] Redirect Logged In Users Away from Home Page (login page)

Home Forums Support [Support request] Redirect Logged In Users Away from Home Page (login page)

Home Forums Support Redirect Logged In Users Away from Home Page (login page)

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1652475
    Gerson

    I’ve created a login form with Elementor on my homepage and I want to redirect all logged in users away from the homepage to a specific page if they attempt to visit the homepage again. I’m using this code and adding it to the hook element in wp_head but I’m having no luck..is this possible?

    <?php 
    add_action( 'template_redirect', function() {
        if ( is_page( 'home' ) &&  is_user_logged_in() ) {
            wp_redirect( home_url( '/presentation' ) );
            die();
        }
    } );
    ?>
    #1652483
    Elvin
    Staff
    Customer Support

    Hi there,

    Do you need it on home/front page only? If so, try this:

    add_action( 'template_redirect', function() {
        if ( is_front_page() &&  !is_user_logged_in() ) {
            wp_redirect( home_url( '/presentation' ) );
            die();
        }
    } );

    I’m using this code and adding it to the hook element in wp_head but I’m having no luck..is this possible?

    Hook element module is for presentation purposes only. For this kind of function you should use a child theme’s functions.php or Code snippets plugin. https://docs.generatepress.com/article/adding-php/

    But if you want it to apply to ALL pages, I think this is the one you’re looking for.
    https://developer.wordpress.org/reference/functions/auth_redirect/

    Example: If you want ALL pages to WordPress’s default login page

    add_action( 'template_redirect', function() {
        if ( !is_user_logged_in() ) {
            auth_redirect();
        }
    } );

    A wise man once said:
    "Have you cleared your cache?"

    #1652513
    Gerson

    Thanks for that. I’m using the following code in functions.php file but its not working:

    //Redirect logged users to presenation page//
    add_action( 'template_redirect', function() {
        if ( is_front_page() &&  !is_user_logged_in() ) {
            wp_redirect( home_url( '/presentation/' ) );
            die();
        }
    } );

    My use case is…when a user logs in and accidentally closes their browser, or navigates away to another website, when they revisit the homepage URL (since they are already logged in) I want it to redirect them to a specific page.

    #1653355
    Tom
    Lead Developer
    Lead Developer

    That code will redirect you to /presentation/ if you’re on the front page and not logged in.

    If you want it to happen when the user is logged in, you need to remove the ! before is_user_logged_in().

    #1653614
    Gerson

    Thanks Tom but every time I do that, I get this error message: “Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.” Is it just a matter of removing the ‘!’?

    #1653670
    Tom
    Lead Developer
    Lead Developer

    Removing the ! shouldn’t cause that. However, if you check your error_log file, you should find a specific error that should help us know the actual cause.

    #1653686
    Gerson

    Hi Tom, I’m not sure which error log it was…but it could be this one:
    generatepress_child/functions.php on line 87″ while reading response header from upstream

    #1653863
    Elvin
    Staff
    Customer Support

    My use case is…when a user logs in and accidentally closes their browser, or navigates away to another website, when they revisit the homepage URL (since they are already logged in) I want it to redirect them to a specific page.

    If this is the use case then I’m not sure its a good idea to use this code at all.

    Using this code means a logged in user will be redirected to /presentation page every time it tries to visit the frontpage which will literally mean logged in users can’t visit the homepage anymore regardless if it’s a revisit or a first time visitor. I’m not sure if this is the one you should use with your use case, unless, that’s what you actually want.

    In this case, I believe what you can do is implement cookies to your site and use that as basis for redirection.

    But this will most likely require scripting or a third-party plugin which is, unfortunately, out of our scope of support.

    A wise man once said:
    "Have you cleared your cache?"

    #1653892
    Gerson

    Using this code means a logged in user will be redirected to /presentation page every time it tries to visit the frontpage which will literally mean logged in users can’t visit the homepage anymore regardless if it’s a revisit or a first time visitor. I’m not sure if this is the one you should use with your use case, unless, that’s what you actually want.

    Actually Elvin, that is exactly what I want to achieve 😀 – is it possible?

    #1653959
    Elvin
    Staff
    Customer Support

    Then this should work:

    add_action( 'template_redirect', function() {
        if ( is_front_page() &&  is_user_logged_in() ) {
            wp_redirect( home_url( '/presentation/' ) );
            die();
        }
    } );

    I basically just removed the !. It’s strange that it caused error on yours.

    I’ve tested the same code on my sandbox site to make sure it doesn’t cause any errors and it actually works as I’ve mentioned.

    I’ve read the error on your site and its to be because the site already loads the header. Ideally we do the redirect before get_header() to avoid this issue.

    Perhaps these articles could help you out.
    https://tommcfarlin.com/wp_redirect-headers-already-sent/
    https://stackoverflow.com/a/8028987

    A wise man once said:
    "Have you cleared your cache?"

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.