Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Redirect to Custom Login Page on Failed Login

Posted on June 3, 2011 by Pippin in Beginner, Quick Tips, Tutorials 14 Comments
Home» Tutorials » Beginner » Redirect to Custom Login Page on Failed Login
Tweet
Love It - 0

A lot of times people wish to place a front end login form on their Website, which helps to hide the fact that the site is running WordPress. This works great, except when a user has a fail login attempt. When that happens, they are automatically directed to the default wp-login.php, which is not good for those wishing to disguise their site. This quick tip shows you how to avoid redirecting to wp-login.php.


This is a quick tip. Check out more Quick Tips

This quick snippet will force WordPress to redirect to the URL that you specify in wp_redirect() when a user fails to login.

1
2
3
4
5
6
7
8
9
add_action( 'wp_login_failed', 'pippin_login_fail' );  // hook failed login
function pippin_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') ) {
          wp_redirect(home_url() . '/?login=failed' );  // let's append some information (login=failed) to the URL for the theme to use
          exit;
     }
}

We also append a $_GET variable, login, and set it equal to failed, which we can use in our theme to display special content only when the user has failed to login.

Tweet Follow @pippinsplugins
login, redirect

14 comments on “Redirect to Custom Login Page on Failed Login”

  1. rob says:
    February 4, 2012 at 4:51 pm

    thanks pippin!

    Reply
  2. Henry says:
    May 21, 2012 at 1:51 pm

    Very nice. Have seen something similar elsewhere but your code works.

    Is there a way to direct to a specific page and not the homepage?

    Reply
    • Pippin says:
      May 21, 2012 at 10:43 pm

      Yes, just replace home_url() with the URL of the page you’d like to redirect to, or use the get_permalink() function.

  3. k3davis says:
    February 25, 2013 at 1:46 pm

    Is there a similar technique that can be used to redirect users on a front-end enabled password reset form, to prevent erroneous entries from redirecting to the back-end reset screen?

    Reply
    • Pippin says:
      February 25, 2013 at 7:46 pm

      I’m not sure I understand what you mean.

    • k3davis says:
      March 28, 2013 at 9:52 pm

      I mean the form you fill out when you forget your password. It’s easy to use a similar method to make a front-end version of this, so that they enter the username/password they don’t have to be taken to a WP style screen to do so. However if they enter a username or password that’s not in WP, they get taken to the forgot password WP screen with an error message, roughly equivalent to the problem you described in this post.

    • Pippin says:
      March 29, 2013 at 1:06 pm

      Gotcha. Yes you could definitely do that, but the feature is not included in the plugin.

  4. federico says:
    March 27, 2013 at 11:24 am

    Hello ! its a very good tip! so.. where have i to past this code ? in functions.php ????

    Reply
    • Pippin says:
      March 27, 2013 at 5:14 pm

      Yep that works just fine.

  5. Patric says:
    March 28, 2013 at 10:10 am

    This code works perfectly, thanks man!

    Reply
  6. maxoud says:
    April 11, 2013 at 2:12 pm

    This will redirect the careless user only when both of login and password fields are set. If at least one of them is empty (blank), this will redirect him to /wp-login.php again. Not a solution to hide WordPress usage.

    Reply
  7. Dan says:
    April 16, 2013 at 5:30 am

    idem maxoud. any thought on that one?

    Reply
  8. Dan says:
    April 16, 2013 at 5:34 am

    I think I found a solution. take a look :

    function wp_authenticate($username, $password) {
    $username = sanitize_user($username);
    $password = trim($password);

    $user = apply_filters(‘authenticate’, null, $username, $password);

    if ( $user == null ) {
    $user = new WP_Error(‘authentication_failed’, __(‘ERROR: Invalid username or incorrect password.’));
    }

    $ignore_codes = array(‘empty_username’, ‘empty_password’);

    if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {

    // Put your code here

    }

    return $user;
    }

    Hope it works !

    Reply
  9. maxoud says:
    April 17, 2013 at 3:35 am

    Dan, here is what i’ve found:
    WordPress login widget redirect prevention.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Front End Registration and Login Forms Plugin
  • Creating Custom Front End Registration and Login Forms for WordPress
  • WordPress Login Form Short Code
  • Sliding Login Form Plugin

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @claudiosmweb I&#039;ve never seen that plugin before, but looks cool :)
    May 22, 2013
  • The iris color picker is almost stupidly easy to implement in #WordPress plugins / themes
    May 22, 2013
  • 2Checkout Gateway for Easy Digital Downloads https://t.co/EcHqXAKUn6
    May 22, 2013

Topics

featured get_user_meta the_content shortcodes contextual help Sugar Event Calendar add_options_page campaign monitor meta box Rémi Corson add_shortcode register_setting wp_enqueue_script authors attachment image plugin forms login short codes do_action Related posts mail chimp apply_filters bbpress comments recent posts post types short code taxonomies custom post type gallery Ajax images Stripe taxonomy jquery users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins