3

I want to redirect an user and his user ID is 6. So I added this code into my functions.php

if ( is_user_logged_in() ) {
    $user_id = get_current_user_id();
      if ($user_id == 6) {
            /* redirect users to front page after login */
            function redirect_to_front_page() {
                global $redirect_to;
                    if (!isset($_GET['redirect_to'])) {
                    $redirect_to = get_option('siteurl');
                    }
                }
            add_action('login_form', 'redirect_to_front_page');             
      }
    }   

But still the user goes to the rpofile page after the log-in. How can I get it works?

[update] Then I've tried to change to username instead of user ID but still same. He gets profile page after login. like this:

function redirect_to_front_page() {
    global $redirect_to;
    if ( is_user_logged_in() ) {
        //$user_id = get_current_user_id();
        $current_user = wp_get_current_user();

          //if ($user_id == 6) 
          if ($current_user->user_login = 'hirer') {
            /* redirect users to front page after login */
            if (!isset($_GET['redirect_to'])) {
                $redirect_to = get_option('siteurl');
            }
        }

    }
}
add_action('login_redirect', 'redirect_to_front_page');  

still nothing changed. The user gets profile page when he login.

  • it looks like your hook(login_form) is wrong. – shyammakwana.me Dec 9 '14 at 8:18
  • What profile page are you talking about? – cybmeta Dec 9 '14 at 9:20
  • @cybmeta this page: www.mysite.com/wp-admin/profile.php – Riffaz Starr Dec 9 '14 at 9:24
  • And where do you want ot redirect the user? – cybmeta Dec 9 '14 at 9:33
  • @cybmeta to www.mysite.com – Riffaz Starr Dec 9 '14 at 9:34
7

You need to use the login_redirect filter returning the redirect location:

add_filter( 'login_redirect', 'redirect_to_home', 10, 3 );
function redirect_to_home( $redirect_to, $request, $user ) {

    if( $user->ID == 6 ) {
        //If user ID is 6, redirect to home
        return get_home_url();
    } else {
        //If user ID is not 6, leave WordPress handle the redirection as usual
        return $redirect_to;
    }

}
  • works like a charm.. thanks a lot.. any why my code doesn't work. Probably that code also should work. right? – Riffaz Starr Dec 9 '14 at 10:00
  • 1
    You was not returning the redirect location and you was trying to redirect only if $_GET['redirect_to'] is not set and $_GET['redirect_to'] is always set unless you access directly to the login form. – cybmeta Dec 9 '14 at 10:13
1

use login_redirect hook instead login_form And you can move conditions inside your function

like below code.

function redirect_to_front_page() {
    global $redirect_to;
    if ( is_user_logged_in() ) {
        $user_id = get_current_user_id();
          if ($user_id == 6) {
            /* redirect users to front page after login */
            if (!isset($_GET['redirect_to'])) {
                return $redirect_to = get_option('siteurl');
            }
        }

    }
}
add_action('login_redirect', 'redirect_to_front_page' , 10 , 3 );             
  • thanks for that. but nothing happened still the user gets profile page after login. I've tried to change to username instead of user ID but still same. He gets profile page after login. check my updated codes. – Riffaz Starr Dec 9 '14 at 9:06
  • may be it's because you have another plugin that's overriding login_redirect action. – shyammakwana.me Dec 9 '14 at 11:49
  • check updated code. – shyammakwana.me Dec 9 '14 at 11:52

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.