Support » Developing with WordPress » Redirect users from register page if logged

  • Resolved cienfu90

    (@cienfu90)


    Hello,

    Is there any chance of redirect users from register page if they are already logged in or someone know how to implement this?

    Thanks.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Most themes provide a body class “logged-in”, I have used this to show/hide menu items. This could help you keep logged in sessions away from the registration screen.

    In php your can use something like this:

    if( is_user_logged_in() ) {
        wp_redirect('/some-other-url');
    }
    Thread Starter cienfu90

    (@cienfu90)

    Hi @vanaf1979

    Where should i place that piece of code?

    May i have to add another if asking if you are in register page and add in functions?

    Thanks for the help.

    Hey @cienfu90

    The simplest way is to just add it at the top of you register template file.

    Or you can add it to your functions php like so:

    add_action('after_setup_theme' , function() {
        if( $_SERVER['REQUEST_URI'] == '/register' && is_user_logged_in() ) {
            wp_redirect('/some-other-url');
        }
    });

    Change /register and /some-other-url to the required slugs/url.

    Let me know if this works for you.

    Stephan

    Thread Starter cienfu90

    (@cienfu90)

    Hi @vanaf1979

    It doesn’t work, still redirecting to the homepage, i try adding it to the functions file on my theme.

    • This reply was modified 1 year, 5 months ago by cienfu90.

    Hey @cienfu90,

    Did you change the url’s inside my code example? the “/regsiter” and the “/some-other-url”? You may need to add a slash at the end like so “/regsiter/”!

    Thread Starter cienfu90

    (@cienfu90)

    Yes sure, i am using yootheme and place it on funcionts theme file.

    /**
     *
     *Redirigir a actividad si el usuario ya esta logeado y hace click en registro
     *
     */
     add_action('after_setup_theme' , function() {
        if( $_SERVER['REQUEST_URI'] == '/registro/' && is_user_logged_in() ) {
            wp_redirect('/actividad/');
        }
    });
    
    Thread Starter cienfu90

    (@cienfu90)

    @vanaf1979 i try with /registro/ and /registro, same, always goes to homepage.

    Moderator bcworkz

    (@bcworkz)

    Try using the “init” action instead. I don’t think WP has quite figured out the user status after theme setup.

    If that still does not help, error_log the $_SERVER[‘REQUEST_URI’] value and check the result to find out why it’s not matching to /registro/ or what ever.

    Thread Starter cienfu90

    (@cienfu90)

    Hi @bcworkz you mean like this, right?

     add_action('init' , function() {
        if( $_SERVER['REQUEST_URI'] == '/registro/' && is_user_logged_in() ) {
            wp_redirect('/actividad/');
        }
    });

    Doesn’t work šŸ™

    Moderator bcworkz

    (@bcworkz)

    You need to follow wp_redirect() with an exit; statement so PHP stops processing the current thread. Otherwise the server thinks it’s still busy.

    Thread Starter cienfu90

    (@cienfu90)

    so it will be like

     add_action('init' , function() {
        if( $_SERVER['REQUEST_URI'] == '/registro/' && is_user_logged_in() ) {
            wp_redirect('/actividad/');
            exit();
        }
    });

    will try it later, thanks

    Moderator bcworkz

    (@bcworkz)

    Correct. If you still have trouble there’s something wrong the the URI values. I double checked on my site (with different URIs of course) and it works as expected.

    Thread Starter cienfu90

    (@cienfu90)

    Works perfect! Thanks!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Redirect users from register page if logged’ is closed to new replies.