I am using the following snippet to control redirects after successful logins....
add_action( 'wp_login', 'redirect_on_login' ); // hook failed login
function redirect_on_login() {
$referrer = $_SERVER['HTTP_REFERER'];
$homepage = get_option('siteurl');
if (strstr($referrer, 'incorrect')) {
wp_redirect( $homepage );
}
elseif (strstr($referrer, 'empty')) {
wp_redirect( $homepage );
}
else
{
wp_redirect( $referrer );
}
}
What i want it to do is this...
- If $referrer is www.mydomain.com/?login=incorrect then redirect to the homepage
- If $referrer is www.mydomain.com/?login=empty then redirect to the homepage
- Anything else then redirect to $referrer
I'm sure there is something wrong with my logic as whatever $referrer is it just redirects me to the same. Am i missing something obvious?
UPDATE
As requested, here is a bit more of an explanation...
- Somebody goes to my custom WordPress login page at www.mydomain.com
- They try to log in with an incorrect password or username
The following function runs...
add_action( 'wp_login_failed', 'pu_login_failed' ); // hook failed login function pu_login_failed( $user ) { // check what page the login attempt is coming from $referrer = $_SERVER['HTTP_REFERER']; $loginpage = 'http://www.mydomain.com/login'; // check that were not on the default login page if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') && $user!=null ) { // make sure we don't already have a failed login attempt if ( !strstr($referrer, '?login=failed' )) { // Redirect to the login page and append a querystring of login failed wp_redirect( $loginpage . '/?login=incorrect'); } else { wp_redirect( $referrer ); } exit; } }
This is how the ?login=incorrect gets added, I am probably going about it the wrong way though
wp_login
action hook correctly. You might want to check the codex. Yourredirect_on_login
function isn't accepting any arguments. – vancoder Apr 12 '13 at 21:36HTTP_REFERER
. It is not reliable, and can be spoofed. – vancoder Apr 12 '13 at 21:38