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

Custom Login Errors and variables I can use

There may be an easier way to do this, but you could rewrite the wp_authenticate_username_password() function in the \wp-includes\class-wp-error.php file.

Of course you cannot edit the function directly, but since this function is a filter function, you can remove the filter, copy it, add the copied function back in and test it. After testing that it still produces the same error, you can then change the error in the copied code.

The following code removes the old WordPress function and creates a new function with a unique name.

remove_filter( 'authenticate', 'wp_authenticate_username_password' );
add_filter( 'authenticate', 'wpse_115539_authenticate_username_password', 20, 3 );
/**
 * Remove WordPress filer and write our own with changed error text.
 */
function wpse_115539_authenticate_username_password( $user, $username, $password ) {
    if ( is_a($user, 'WP_User') )
        return $user;

    if ( empty( $username ) || empty( $password ) ) {
        if ( is_wp_error( $user ) )
            return $user;

        $error = new WP_Error();

        if ( empty( $username ) )
            $error->add( 'empty_username', __('<strong>ERROR</strong>: The username field is empty.' ) );

        if ( empty( $password ) )
            $error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) );

        return $error;
    }

    $user = get_user_by( 'login', $username );

    if ( !$user )
        return new WP_Error( 'invalid_username', sprintf( __( '<strong>ERROR</strong>: Invalid username. <a href="https://wordpress.stackexchange.com/questions/115539/%s" title="Password Lost and Found">Lost your password</a>?' ), wp_lostpassword_url() ) );

    $user = apply_filters( 'wp_authenticate_user', $user, $password );
    if ( is_wp_error( $user ) )
        return $user;

    if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) )
        return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="https://wordpress.stackexchange.com/questions/115539/%2$s" title="Password Lost and Found">Lost your password</a>?' ),
        $username, wp_lostpassword_url() ) );

    return $user;
}

The next step is to test the code to make sure it is producing the same error as before. Once tested, try changing the error message as you wish. If things go too wrong, start over or comment out the first 2 lines and the WordPress function will use the old error message.

// remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
// add_filter( 'authenticate', 'wpse_115539_authenticate_username_password', 20, 3 );

Related Posts:

  1. Editing ‘Password Reset’ E-mail
  2. Change “You are now logged out” text
  3. Removing labels and tag on WordPress’s default login form
  4. Customize user account activation message
  5. How do we check if the user is logging in or registering?
  6. add filter login_redirect does not contain original requested redirect
  7. Change username before login
  8. Correct method of redirecting user login
  9. Filters on Login Page
  10. How can I change the email that is inside the default email texts of wordpress?
  11. How do I hook my function to run only after submitting login form
  12. Change login CSS
  13. How to redirect non-logged in users to login with post_password_required()
  14. Filter page title (displayed in browser tab) of wp-login
  15. How to redirect a unique link based on login status
  16. login_url filter creates permanent wp-admin -> wp-login redirection loop
  17. Login logout below menu bar
  18. Hook into ‘when user logs in’ [persistent login]
  19. Change password reqts with NO plugin without breaking resetpass link?
  20. How to modify Contact Form 7 Success/Error Response Output [closed]
  21. Filter media library items by size
  22. xmlrpc_enabled filter not called
  23. Change the footer text on the login page
  24. Anyway to edit the titlebar of WordPress Widgets in the Admin area?
  25. How may I filter get_users() similarly to pre_get_posts?
  26. Using hooks to place content in theme dynamically
  27. wp_title() empty on a static front page
  28. Search pre_get_posts filter which can handle multiple post types and categories?
  29. End excerpt at the end of the sentence
  30. Customize WooCommerce Error Message
  31. Dump contents of a variable inside a filter/hook callback that runs during save post
  32. Conditionally call add_action depending on post_type?
  33. Right way to add HTML bloginfo name using a filter
  34. Allow users to exclude categories? (WP 3.1.2)
  35. Filter category in WooCommerce shop page to display related sub-category
  36. Where can you find the available filter tags?
  37. Is possible dequeue/remove style from wp_footer() hook and add on wp_head() hook?
  38. Applying custom filter instead of the_content doesn’t render embeds
  39. Removing an action, or dequeueing style – Both not working
  40. Where is the changelog for 3.5? where is the list of added removed files?
  41. De-bloating the page classes
  42. Move a file from a directory to another
  43. Echoing Categories only if they’re a child of a given category
  44. Adding a title prefix with the the_title filter
  45. How to removes all instances of thumbnails displayed in my theme?
  46. How to modify an existing meta description?
  47. Adding TinyMCE buttons without removing plugin buttons?
  48. Display Externally Retrieved Data within a Custom Template
  49. Can the wordpress color palettes by changed through Javascript?
  50. Filter on the_content doesn’t update the content being searched via register_rest_route
  51. Where to add_filter in theme functions.php?
  52. tag removed not using a filter
  53. How to customize the “Insert/edit link” popup box?
  54. Give “Read More” precedence over excerpt() word count
  55. What hooks to hook onto for automatic cache clearing
  56. Add Bootstrap Classes to Recent Posts Widget
  57. Add filter to get_posts
  58. How can I output all apply_filters and do_action?
  59. Override has_post_thumbnail function
  60. How to allow code block in wordpress comments
  61. Is it possible to override the default Gallery Settings form?
  62. remove_action: how to access to a method in an child class?
  63. Set default value for “Additional CSS class(es)” for specific block types
  64. WordPress add script with custom type (x-jquery-tmpl)
  65. Woocomemrce order and registration fileds to UPPERCASE
  66. add_filter to wp_nav_menu_objects having no impact
  67. How to add nonce tag to inline script for CSP
  68. Filter content for get_the_content()
  69. How to override include_once pointed file using add_filter?
  70. How to strip out javascript in wp_posts `post_content`
  71. WordPress removing slashes from shortcode output
  72. Register new user, assign custom role then send email
  73. pre_get_posts to hide everywhere posts from “Archive” category
  74. How to remove shortcode(s) from WYSIWYG Page editor for Non Admins
  75. What is the action or filter for adding information under the Permalink in Edit Post/Page?
  76. add after content don’t work
  77. Adding a filter to related posts not working
  78. need correction with a snippet in functions.php [closed]
  79. conditional search
  80. output custom Posts on a Page
  81. Sql query does not work well
  82. WordPress Gutenberg core/heading block (wp-block-heading)
  83. Why does wp_die() not work when inside a namespace?
  84. Apply pre_get_posts filter in a certain moment (just for a particular block of posts)
  85. Modify oembed code conditionally
  86. Set post featured image to author image
  87. How to Change the WordPress Locale with the ‘locale’ Filter?
  88. meta box loop problem with a search filter
  89. Modify Default URL for /wp-includes/js/wp-emoji.js?ver=4.6.1
  90. wp_editor customization
  91. Is it possible to include the add_filter() function within a shortcode function
  92. Activate short codes for all post queries?
  93. Save something to global var in add_filter
  94. filter on the_content stopped working when I updated to WP 3.6.1
  95. Replace Paid Shipping Method With Free Shipping Method WooCommerce [duplicate]
  96. Wrapping Featured Image on Add/Edit Page in div?
  97. Clean/filter HTML inserted to post content by XML RPC
  98. how to localize the number of wordpress post views?
  99. Is it possible to make get variable out of filter in Class?
  100. How do you disable the verification process of user email changes?
Categories filters Tags filters, login
Filter Posts by current Month
Diplay content from custom type as widget or sidebar?

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