Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Created October 30, 2014 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadawais/498768e5f22b4b632e18 to your computer and use it in GitHub Desktop.
Save ahmadawais/498768e5f22b4b632e18 to your computer and use it in GitHub Desktop.
Custom Login/Logout handling for using WordPress as a CMS
/*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