Created
October 30, 2014 17:09
-
-
Save ahmadawais/498768e5f22b4b632e18 to your computer and use it in GitHub Desktop.
Custom Login/Logout handling for using WordPress as a CMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Login Error Handle*/ | |
add_action( 'wp_login_failed', 'aa_login_failed' ); // hook failed login | |
function aa_login_failed( $user ) { | |
// check what page the login attempt is coming from | |
$referrer = $_SERVER['HTTP_REFERER']; | |
// 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( $referrer . '?login=failed'); | |
} else { | |
wp_redirect( $referrer ); | |
} | |
exit; | |
} | |
} | |
/*Login Empty Fields Error handling*/ | |
add_action( 'authenticate', 'pu_blank_login'); | |
function pu_blank_login( $user ){ | |
// check what page the login attempt is coming from | |
$referrer = $_SERVER['HTTP_REFERER']; | |
$error = false; | |
if($_POST['log'] == '' || $_POST['pwd'] == '') | |
{ | |
$error = true; | |
} | |
// check that were not on the default login page | |
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') && $error ) { | |
// 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( $referrer . '?login=failed' ); | |
} else { | |
wp_redirect( $referrer ); | |
} | |
exit; | |
} | |
} | |
/*Logout Redirect*/ | |
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 ); | |
function go_home(){ | |
wp_redirect( home_url('/login/') ); | |
exit(); | |
} | |
add_action('wp_logout','go_home'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment