logout - How to redirect returning users who previously logged in?

admin2025-04-19  0

There are many answers to how to redirect after login. But my situation is different. In my scenario, we have a landing page where people can login. Once logged in, the users are not logged out for a year. So, what we want to do is, when the user visits the site, check if they are logged in. If they are logged in, automatically redirect to a specific page. I used a plugin to embed this code on the landing page, but it does nothing :

<?php
if (is_user_logged_in()) {
wp_redirect( '/specific-page/' );
exit;
} 

Edit: I added echo statements before entering the if block, after entering the block, after calling wp_redirect, and, after exit. When I look at the page source, all the echos are printing, except the one after exit.

There are many answers to how to redirect after login. But my situation is different. In my scenario, we have a landing page where people can login. Once logged in, the users are not logged out for a year. So, what we want to do is, when the user visits the site, check if they are logged in. If they are logged in, automatically redirect to a specific page. I used a plugin to embed this code on the landing page, but it does nothing :

<?php
if (is_user_logged_in()) {
wp_redirect( 'https://our.domain/specific-page/' );
exit;
} 

Edit: I added echo statements before entering the if block, after entering the block, after calling wp_redirect, and, after exit. When I look at the page source, all the echos are printing, except the one after exit.

Share Improve this question edited Oct 20, 2019 at 8:25 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Oct 20, 2019 at 6:54 SanSoloSanSolo 1035 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 0

Maybe these links can help you:

  • https://stackoverflow/questions/9191359/how-to-change-session-expire-time-in-wordpress
  • https://www.wpbeginner/plugins/how-to-redirect-users-after-successful-login-in-wordpress/

I'm not very experienced with PHP, but I suspected this has something to do with headers. If I embed the script in a page, it is trying to redirect AFTER the page is rendered. So, I used a plugin called Code Snippets to add this to the functions.php:

function my_custom_function() {
    if(is_front_page()){ 
        echo "before the block";
        if (is_user_logged_in()) {
            echo "user is logged in";
         wp_redirect( "https://my.domain/page/");
            echo "after redirect";
          exit;
            echo "after exit";
    }
}
}
add_action( 'wp_enqueue_scripts', 'my_custom_function' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745066994a283045.html

最新回复(0)