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.
2 Answers
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;
}
}