Redirect on successful login

According to the Codex page for wp_redirect(), you should follow your wp_redirect() calls with exit.

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 );
        exit;
    } elseif (strstr($referrer, 'empty')) {
        wp_redirect( $homepage );
        exit;
    } else {  
        wp_redirect( $referrer );
        exit;
    }
}

If that doesn’t work, try commenting out your wp_redirect() calls and then echo( $referrer ); to see if $referrer is set correctly.

Leave a Comment