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

Check for empty username or password on login

Part of the problem is you’ve set the priority of your filter to “1” which means it will run first. Since authenticate is going to run on any login (wp-login.php or your front-end login page), you probably need to start with setting it to the default (10). Otherwise, it’s just going to hijack your wp-login.php.

Since you don’t need to do this on wp-login.php, you could also check to make sure that’s not the current page when you run your custom validation. I did that in your function by checking the global ‘pagenow’ is not ‘wp-login.php’.

function verify_user_pass( $user, $username, $password ) {
    if ( $GLOBALS['pagenow'] != 'wp-login.php' ) {
        $login_page  = home_url( 'login/' );
        if ( "" == $username || "" == $password ) {
            wp_redirect( add_query_arg( 'login', 'empty', $login_page ) );
            exit();
        }
    }

    return $user;
}
add_filter( 'authenticate', 'verify_user_pass', 10, 3 );

Some other things I changed:

  • home_url() does not need a leading slash for slugs – it will put that in automatically.
  • changed your comparisons to “yoda conditions” ("" == $username) – it’s a good habit to get into because it catches type/syntax errors (like if you mistype “=” instead of “==“).
  • use add_query_arg() to add your query argument to the URL. That way it will be added properly, even if there are other query arguments in the URL.
  • Note that this is a filter, not an action. Therefore, you need to make sure you return what the filter hook is filtering. In the case of authenticate that’s the $user object (which may also contain a WP_Error object). If you don’t return this at the end, you’ll break anything outside your custom process that also runs this filter (i.e. the main wp-login.php).

Related Posts:

  1. Replacing the WordPress password validation
  2. WordPress auto login after registration not working
  3. Authenticate user using Hashed Password in MySQL Query and C#
  4. Is it possible to get a user with just the password field?
  5. My custom page template with is_user_logged_in() does not detect that I’m logged in
  6. How can I allow password reset based on logins containing the @ character?
  7. Reset Password policy
  8. OAuth 2 and saving the authenticated user
  9. WordPress Login Customization for External Authentication
  10. REST API: wp_get_current_user not working on second call
  11. How do you manage your pages or functions that require logged-in users?
  12. How can i login with user’s password in WordPress being an admin?
  13. The same session information for peer users on two different WordPress servers
  14. WP users cant reset password
  15. Securely log in a user without a password using a link?
  16. How to track all users logged into a site?
  17. How to authenticate/verify login credentials & check for user meta without logging in?
  18. How do I require authorization / login to view a specific set of posts / pages?
  19. how to use joomla password format in wordpress?
  20. Allow up to 5 Concurrent Login Sessions
  21. How to let user set password on registration
  22. Best way to send users password?
  23. Why does is_user_logged_in() return false after redirect from another site?
  24. Setting WP Admin passwords to expire
  25. Pre-populate Username Field
  26. Migrating users from .com to .org?
  27. How to keep track of user logins?
  28. Get user info outside WordPress
  29. How can I secure a WordPress blog using OpenID from a single provider?
  30. Redirect after login based on user role (custom login page)
  31. Check for user meta data at Login
  32. Redirect User to Homepage if no other redirect is specified
  33. Use phpbb user database for WordPress
  34. Importing Existing Users with Passwords
  35. WordPress to use Drupal users’ credentials
  36. Authenticate with a Rails app?
  37. Cannot set user passwords
  38. Share user table from WP with Drupal
  39. A way to count logged in users and display count?
  40. stop login if user_status equal zero
  41. WordPress Authentication Middleware
  42. Show MD5 Password in user section
  43. How do i make my wordpress website private?
  44. How do I get WordPress login to ignore the password input if a particular username is used?
  45. Redirect user to login before viewing custom post
  46. Force user to change their password on first log in of site using shortcode
  47. Change the user_login at registration
  48. Max no of simultaneous active sessions for a single user
  49. Rest API code to get ID of current user not working: get_current_user_id() gives 0
  50. When I try to login in wordpress it is showing “USER Doesn’t Exists”
  51. Force user to change their password on the frontend at the first login and password policy
  52. Should I encrypt the response that triggers an Ajax action? Is nonce sufficient?
  53. Redirect subscribers to last viewed page after log-in
  54. Can i add password field into my wp registration form?
  55. wordpress disable login for unverified user
  56. How do I transfer user passwords from one WordPress site to another?
  57. Rewrite Rules and Login Issue
  58. How can I authenticate user credentials against a WordPress instance?
  59. How do I protect user_activation_key?
  60. Is possible to allow user to login with different role?
  61. Use WordPress Login for a non-wordpress site
  62. Allow users to create post without logging in?
  63. Does wordpress support natively the concept of logging-in users? (not admins, but users of the website)
  64. New user form rejected because “passwords don’t match”—but there’s only one password field
  65. Is there any action /filter hook I can use to disable login for some user role?
  66. WordPress Password security related questions
  67. How to check User Role and redirect to specific page according to Role when login in WordPress?
  68. Display video on homepage for users who have not logged in
  69. Send clear password via mail
  70. Delete a user from frontend
  71. Force users to use password with specifications
  72. Check if user is logged in via JS? [duplicate]
  73. Use members from 1 site on another one
  74. Can I edit the database to change a login?
  75. how redirect users to custom login page when “login to reply” is clicked? [duplicate]
  76. User(s) already exists show error please provide a valid username
  77. Making WordPress available while logged into another website
  78. wp_generate_password sets password but can’t login using created password
  79. Allow Weak Passwords
  80. add class to element if user is not logged in [closed]
  81. How to verify which WordPress user requested the API in ASP .NET Core?
  82. Need to use WordPress page as authentication for different service
  83. Custom Password Reset
  84. Is there a way to call via javascript if a user is logged-in on a static html file?
  85. Restricting wordpress login sessions for a web app
  86. Send user auto generated password on different email
  87. change user password REST API
  88. How to allow access based on the user meta flag
  89. Authentication between two different sites using the WordPress login cookie
  90. Use WP cookie to authentificate user on an external app
  91. Moving users from joomla to wordpress
  92. How to connect wordpress user with my own APP user?
  93. How to change default username field after login
  94. wordpress Account Management generate password missing on my laptop
  95. Disable emails for new user registration
  96. $user_login is not working if author has only 1 post
  97. Specific Content on pages based on user
  98. user and usermeta table not found
  99. throttle/limit a logged in user’s http requests to specific page on a per day basis
  100. Users set passwords but cannot login
Categories users Tags authentication, login, password, users
HOW do you Redirect buddypress login to EDIT tab not PROFILE tab on profile page? [closed]
How to search posts by title with special characters in WP_Query?

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