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( '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.

0

Maybe these links can help you:

|improve this answer|||||
  • Thanks but no. I have already taken care of the session expiry. The second one, I'm talking about a returning user. Not a user who logs in right now. – SanSolo Oct 20 '19 at 7:05
  • Then try debugging why it's not working. if(is_user_logged_in()){ echo 123; die(); } does this show something? – Кристиян Кацаров Oct 20 '19 at 7:10
  • It does not echo anything. – SanSolo Oct 20 '19 at 7:13
  • where have you placed the code? Is there caching on the website? try first lines from functions.php in your theme – Кристиян Кацаров Oct 20 '19 at 7:14
0

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' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.