0

I am a newbie to Wordpress. I am currently working on registration and login. I have a page restrict url let http://xyz.com/abc. Login/registration is required for this url.

I need that when a user register or login then it should be redirected on this previous url.

In login I have added the following hook :

add_filter('login_redirect', 'redirect_previous_page', 10);

function redirect_previous_page(){

    global $user;
    $request = $_SERVER["HTTP_REFERER"];

    if ( in_array( $user->roles[0], array( 'administrator') ) ) {

        return admin_url();
        return $redirect_to;

    } elseif ( in_array( $user->roles[0], array( 'subscriber') ) ) {

        return $request;
        return $redirect_to;

    } 

}

It is working for me, but I am not getting how can I redirect on this url after successful registration. I have registration link on the login page

2
  • You can't have two return one after the other. Your functions ends on the first return it meets. You also need arguments in your function. Read the codex for an example.
    – RRikesh
    Commented Aug 6, 2013 at 11:48
  • @RRikesh then is it possible that I can send the value of that url along with the register url, and if then how, I am using theme-my-plugin ...
    – lord_linus
    Commented Aug 6, 2013 at 11:53

1 Answer 1

1

You used in_array wrong. There might be some redundancy in your function too(depending on what you're doing). You could use something like this to redirect your users:

add_filter('login_redirect', 'redirect_previous_page', 10);

function redirect_previous_page(){
    global $user;
    if ( in_array( 'administrator', $user->roles ) ) {
        return admin_url();
    } else {
        return 'http://localhost/'; //add your link here
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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