Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Member Area Login with Fail Message

You have to take two scenarios into account because they behave little differently. Copy both of these code blocks to your functions.php file. Code about errors is below, make sure to read comments.


1. Login failed – username and password was provided but something was incorrect

add_action( 'wp_login_failed', 'login_failed_wrong_data' );

function login_failed_wrong_data( $user ) {

    // Get the URL user came from, a.k.a that same login page
    $referrer = $_SERVER['HTTP_REFERER'];

    // Check that we're not on the default login page + other things
    if( ! empty( $referrer ) && ! strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) && $user != null ) {

        // Check if we don't already have a failed login attempt
        if( ! strstr( $referrer, '?login=failed' ) ) {

            // Redirect to same login page and append a query string
            wp_redirect( $referrer . '?login=failed' );
        } 
        else {

            // Redirect to same login page
            wp_redirect( $referrer );
        }
    }

    exit;
}

2. Login failed – username and/or password was not provided at all

add_action( 'authenticate', 'login_failed_no_data' );

function login_failed_no_data( $user ) {

    // Get the URL user came from, a.k.a that same login page
    $referrer = $_SERVER['HTTP_REFERER'];

    $error = false;

    // Check if inputs are empty - these have to match with your input "name" attributes
    if( $_POST['log'] == '' || $_POST['pwd'] == '') {

        $error = true;
    }

    // Check that we're not on the default login page + other things
    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 same login page and append a query string
            wp_redirect( $referrer . '?login=failed' );
        } 
        else {

            // Redirect to same login page
            wp_redirect( $referrer );
        }

        exit;
    }   
}

Updated error message as shortcode:

// Add this to functions.php
add_shortcode( 'loginerror', 'myErrorShortcode' );

function myErrorShortcode() {

    if( isset( $_GET['login'] ) && $_GET['login'] == 'failed' ) {

        // Start "recording"
        ob_start(); ?>

        <div id="login-error">
            <p>Your login attempt was not successful. Please try again.</p>
        </div> <?php

        // Return result
        return ob_get_clean();
    }
}


//How to use
[loginerror]

I suggest to do some research and try something yourself next time before asking from community.

Related Posts:

  1. ‘Password field is empty’ error when using autofill in Chrome
  2. Integrate recaptcha and wp_signon – what is needed?
  3. Receiving “This content cannot be displayed in a frame” error on login page
  4. Warning: Cannot modify header information – headers already sent
  5. wp-admin seems to be redirecting
  6. Can’t Login to WordPress, No Data Received Error
  7. Customizing login error messages
  8. screwed-up my blog..what should I do
  9. How can I find the login page? It was lost after moving the site
  10. Show errors on custom login form [duplicate]
  11. Why would the login page reload indefinitely?
  12. WordPress error on log out ‘Not Permitted’ and can’t log out
  13. Creating custom login errors
  14. Why is wp-login trying to send an email?
  15. Can not login to wordpress site after resolving white screen of death
  16. I can’t access my WordPress dashboard – shows Warning message [closed]
  17. Access log “POST /wp-login.php HTTP/1.0” 400
  18. ERROR: Cookies are blocked due to unexpected output (no FTP access)
  19. Login error ” There has been a critical error on this website”
  20. Unable to login my wordpress website
  21. Help! ERROR: Cookies are blocked due to unexpected output on attempting to login to resolve an issue with my site
  22. Can’t login to wordpress, got ERR_EMPTY_RESPONSE after a few minutes
  23. ERROR: Cookies are blocked due to unexpected output – ultimate solution
  24. Login screen keeps resetting?
  25. How to place wp-login.php in page or page template?
  26. Trying to create a log in system but getting error “Parse error: syntax error, unexpected ‘else’ (T_ELSE) ” [closed]
  27. index.php file shown when trying to load wesite
  28. In Django, how do I know the currently logged-in user?
  29. Can I programmatically login a user without a password?
  30. wp_enqueue_script was called incorrectly
  31. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  32. Is there any way to rename or hide wp-login.php?
  33. How to login with email only no username?
  34. How can I redirect user after entering wrong password?
  35. Increase of failed login attempts, brute force attacks? [closed]
  36. Login page ERROR: Cookies are blocked due to unexpected output
  37. Separate registration and login for different roles
  38. SSO / authentication integration with external ‘directory service’
  39. Preventing session timeout
  40. How reduce wordpress login session timeout time?
  41. How to prefill WordPress registration with social details
  42. Check for correct username on custom login form
  43. Disallow user from editing their own profile information
  44. I can’t access my site via wp-admin
  45. Removing username from the ‘wordpress_logged_in’ cookie
  46. How to show ‘login error’ and ‘lost password’ on my template page?
  47. What is $interim_login?
  48. Custom login form
  49. How to prefill the username/password fields on the login page
  50. wp_signon returns user, but the user is not logged in
  51. Adding extra authentication field in login page
  52. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  53. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  54. wp_signon() does not authenticate user guidance needed
  55. What exactly is ReAuth?
  56. What are the differences between wp_users and wp_usermeta tables?
  57. Login members using web services
  58. Make my wordpress blog remember my login “forever”
  59. How to check in timber if user is loggedin?
  60. How do I change the language of only the login page?
  61. Disable WordPress 3.6 idle logout / login modal window / session expiration
  62. Stop WordPress from logging me out (need to keep me logged in)
  63. Woocommerce registration page [closed]
  64. How to disable autocomplete on the wp-login.php page
  65. Share login data/cookies between multiple installations
  66. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  67. How to pass users back and forth using session data?
  68. How do I change the logo on the login page?
  69. Why does WordPress hide the reset password key from the URL?
  70. Is it possible to sign in with user_email in WordPress?
  71. How to use current_user_can()?
  72. Avoid to load default WP styles in login screen
  73. WordPress registration message
  74. How to fake a WordPress login?
  75. how to display the wordpress login and register forms on a page?
  76. Does wp_logout_url() destroy a session? (Logging out question)
  77. How can I send a welcome email to a user AFTER they login for the first time?
  78. Can not login with correct username and password
  79. Website Visible only to Registered users
  80. How can i increase the login expiration length?
  81. How do I use add_action from a class method?
  82. How to remove the WordPress logo from login and register page?
  83. How can I add a custom script to footer of login page?
  84. Brute force attack?
  85. Customize wp_new_user_notification_email()
  86. Need to execute a cron job
  87. Login email after registration never sent or received
  88. How can I create a separate blog that is private?
  89. How to keep always logged in development environment
  90. Add Confirm Password field in wp-login.php Password Reset page
  91. How to get Login Error messages on a custom template
  92. Stop users from logging in from multiple locations
  93. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  94. custom login page redirect to logged in user profile page
  95. Email address or username used to login in wordpress
  96. How do I check if a post is private?
  97. Front-end login: Redirect user to the post they had created
  98. My login form does not work
  99. Programmatically log in a wordpress user
  100. Action wp_login_failed not working if only one field is filled out
Categories login Tags errors, login
is it possible to create cart functionality without woocommerce? [closed]
How to get the attribues (alt and title) of an image import with ACF [closed]

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress