Title: Custom login form code causing errors
Last modified: August 30, 2016

---

# Custom login form code causing errors

 *  [johnsimoneau](https://wordpress.org/support/users/johnsimoneau/)
 * (@johnsimoneau)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/custom-login-form-code-causing-errors/)
 * Hi!
 * One of our prior developers used this code in our functions file for our custom
   login form redirects
 *     ```
       // Redirect user in case failed auth from login page
       add_action( 'wp_login_failed', 'my_front_end_login_fail' ); // hook failed login
   
       function my_front_end_login_fail( $username ) {
       $referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from?
       // if there’s a valid referrer, and it’s not the default log-in screen
       if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
       if ( !strstr($referrer,'?login=failed') ) { // make sure we don’t append twice
       wp_redirect( $referrer . '?login=failed' ); // let’s append some information (login=failed) to the URL for the theme to use
       } else {
       wp_redirect( $referrer );
       }
       exit;
       }
       }
   
       global $wp_version;
       if ( version_compare( $wp_version, '3.0', '>=' ) ) :
       	add_theme_support( 'automatic-feed-links' );
       else :
       	automatic_feed_links();
       endif; 
   
       // Redirect user in case failed auth from login page
       add_action( 'authenticate', 'my_custom_function');
       function my_custom_function(){
       $referrer = $_SERVER['HTTP_REFERER'];
       if ( strstr($referrer,'login') && $user==null ) {
       if ( !strstr($referrer,'?login=nodata') ) {
       wp_redirect( $referrer . '?login=nodata' );
       } else {
       wp_redirect( $referrer );
       }
       } }
       ```
   
 * Problem is the function titled my_custom_function is causing users who click 
   on the request new password link to hit an error page after submitting their 
   email address. It says too many redirects and the address it’s going too is [http://our-website-here.com/wp-login.php?action=lostpassword?login=nodata](http://our-website-here.com/wp-login.php?action=lostpassword?login=nodata)
 * When I remove that code it’s successful and redirects to here [http://our-website-here.com/wp-login.php?checkemail=confirm](http://our-website-here.com/wp-login.php?checkemail=confirm)
 * Any ideas how I could keep whatever necessary functionality is there but change
   the coding so we don’t get the errors?
 * Thanks

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/custom-login-form-code-causing-errors/#post-6521280)
 * I’m not entirely sure what this code is trying to do, but adding a condition 
   to one of the `if()` statements to prevent the logic from being applied when 
   the referrer contains the lostpassword parameter seems like it should work. Try
   adding `&& !strstr( $referrer,'?action=lostpassword')` to one of the `if()` statements
   in `my_custom_function()`.
 * Which one? I’m not sure. Try the second one. If that doesn’t work try the other.
   That may not work either. Sorry I can’t be more definitive, but it’s at least
   worth a try.

Viewing 1 replies (of 1 total)

The topic ‘Custom login form code causing errors’ is closed to new replies.

## Tags

 * [login](https://wordpress.org/support/topic-tag/login/)
 * [password reset](https://wordpress.org/support/topic-tag/password-reset/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/custom-login-form-code-causing-errors/#post-6521280)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
