This code checks if the user is registered for the first time, that is, after registration. If so, I want to redirect it to a user page. Otherwise, redirect it to the home page or admin page.
function mylogin_redirect() { global $user_ID; if( $user_ID ) { $user_info = get_userdata( $user_ID ); // If user_registered date/time is less than 48hrs from now // Message will show for 48hrs after registration if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) { header("Location: http://example.com/custompage"); } elseif( current_user_can( 'manage_options' )) { header("Location: http://example.com/wp-admin/"); } else { header("Location: http://example.com/"); } } } add_action('wp_head', 'mylogin_redirect');
But that won't work? I assume that it does not fall into wp_head ... I tried using the login_redirect filter:
function mylogin_redirect($redirect_to, $url_redirect_to = '', $user = null) { global $user_ID; if( $user_ID ) { $user_info = get_userdata( $user_ID ); // If user_registered date/time is less than 48hrs from now // Message will show for 48hrs after registration if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) { return get_bloginfo('url') . "/custompage/"; } elseif( current_user_can( 'manage_options' )) { return admin_url(); } else { return get_bloginfo('url'); } } } add_filter('login_redirect', 'mylogin_redirect');
Although he logs in, he misses me, except for http://example.com/wp-login.php instead of a blank page.
UPDATE: Alright, I don't know what is going on. Using the filter hook, I can only reach the designated destination after the second entry. Well, not really the second login, but the second click of the login button. I did it like this: enter credentials β login β (wrong page) β press return button β enter credentials again β login β (correct page). Weird
wordpress
Joann
source share