0

I have a home/static page with login button. Currently I've set the button url as "http://example.com/login"

How can I set the login button to redirect to a specific page IF/WHEN the user is logged in?

I do not know much about coding. I found some solution about add_action or something and added to theme's function.php. But I can't get it work.

Summary: if non-login user: login button url ---> login page if login user: login button url ---> specific page

  • Do you have the separate page template for login page? – dhirenpatel22 Oct 8 '18 at 8:11
  • @dhirenpatel22 Yes, I've created a separate for the login page. I'm using Ultimate Member plugin to manage membership pages – Aaron W. Oct 8 '18 at 9:54
  • I have found a solution and added as separate answer below...please check and let me know if it works correctly. – dhirenpatel22 Oct 8 '18 at 12:16
0

Add below code in your functions.php file of active theme directory in order to restrict the login page to logged-in users and redirect them to core user page (user profile). You can replace "um_get_core_page( 'user' )" with any page URL where you want to redirect logged-in users.

/* Restrict Login page to logged-in users and redirect to core user page (user profile) */
add_action( 'template_redirect', 'um_restrict_login_page_logged_in' );
function um_restrict_login_page_logged_in() {
    if ( um_is_core_page('login') && is_user_logged_in() ) {
        wp_redirect( um_get_core_page( 'user' ) );
        exit;
    }
}

Hope this works!!

  • this works!! Thanks a lot. But what if my theme got an update? Do i need to place the code every time my theme updates? – Aaron W. Oct 8 '18 at 15:01
  • Use the concept of the child theme and activate the child theme. You can add above code in the functions.php file of the child theme, so even if you update your main parent theme, it won't affect your website and the code will remain as it is in child theme. For child theme refer codex.wordpress.org/… – dhirenpatel22 Oct 8 '18 at 16:31
0

Okay so you are asking if user logged in then redirect to other pages or something like this. It's WordPress pre-built functionality. you can check it out here. You can copy and paste my code in your functions.php if you don't understand from reference code.

add_action( 'init', 'check_user_loggedin' );
function check_user_loggedin(){
    if ( is_user_logged_in() ) {
       wp_redirect( 'https://www.google.com' );
    } else {
        echo 'Welcome, visitor!';
    }
}

Description:

here is_user_logged_in() is checking user login status and if user login status is true then redirect to link or do whatever you like or if status is false then do something else.Here wp_redirect is WordPress default functionality for redirecting to specified link.

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.