Redirect customer from page X to Y if logged in

Hi,

I’ve seen a similar question on these forums, but not quite what I’m looking for. And I don’t know enough about php to fiddle around with it to get exactly what I want.

I’m looking for a solution to redirect already logged in users, customers to my woocommerce site specifically, from page X to Y. So not when logging in but when they are already logged in.

The homepage of my site is URL/login but I don’t want already logged in users to be able to visit this page but I want them to be sent from URL.com or URL/login to URL/account.

Perhaps even URL/wp-admin, because customers will then go to URL/account and admins will go to the WordPress dashboard.

Hope somebody can help me with this.

  • Wouter
    • The Incredible Code Injector

    Hey Jude,

    We meet again! :smile:

    Thanks for mentioning the plugin. Unfortunately that doesn’t really do the trick. It will only set a redirect for when a user logs in and any future log ins.

    But what I’m looking for is a way do redirect a user who is already logged in. Non-logged in users should be able to visit page X, but already logged in users should not, and be redirected to page Y.

  • Jude
    • DEV MAN

    Hi again Wouter

    Hope all is well with you too, glad to meet again and sorry about the delay !

    This will need a bit of code to do perfectly, not sure if a plugin can do it out of the box. Take a look at the code below.

    function n2313_redirect_signup() {
    global $pagenow;

    if ( site_url('page1') == $pagenow ) {

    if ( is_user_logged_in() ) {
    wp_redirect( site_url('page1') );
    exit();
    }

    }
    }
    add_action( 'init', 'n2313_redirect_signup', 500 );

    Replace page1 and page2 with your own urls and you should be set

    Cheers

    Jude

  • Wouter
    • The Incredible Code Injector

    Hey,

    Thanks for the code. Unfortunately it didn’t work for me (there was no ‘page2’ in your code by the way, but twice a ‘page1’ but that’s not why).

    Perhaps because of some other redirections I’ve got going on the site, I don’t know.

    But the good news is, I’ve managed to achieve what I wanted by changing the homepage and sending logged in users from page X (the homepage) to page Y. And because of that non-loggedin users are automatically sent to page Y. Don’t know if you can follow me :smile:, but I fixed it.

    Thanks again!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.