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

Permit Login if table row exists

So I was able to work out a solution that works.

Here is what I did.

First I hooked into the authenticate filter

 add_filter( 'authenticate', 'check_user_is_in_custom_table', 30, 2 );

From there I created my function

 function check_user_is_in_custom_table( $user, $username, $password ) {

Loaded the WordPress Database

 global $wpdb;

Created my result variable

 $result = $wpdb->get_row( $wpdb->prepare( "SELECT User FROM custom_table WHERE User = %s", $username ) );

I had to edit my if statement a bit

 if ( is_null ( $result ) ) { //This is the edited line
   return new WP_Error( 'deactivated_in_custom_table', 'This account has been deactivated.' );
 }
 return $user;

And finally closed the function

 }

Here is the full code for anyone who wants to use this for their own reference.

 add_filter( 'authenticate', 'check_user_is_in_custom_table', 30, 2 );
 function check_user_is_in_custom_table( $user, $username, $password ) {
    global $wpdb;
    $result = $wpdb->get_row( $wpdb->prepare( "SELECT User FROM custom_table WHERE User = %s", $username ) );
      if ( is_null ( $result ) ) {
        return new WP_Error( 'deactivated_in_custom_table', 'This account has been deactivated.' );
      }
      return $user;
 }

Here is what happens, the login form is ran through the validation we have created utilizing the custom table within the WordPress database. If there is a row within the table where the USer is the username provided on sign on then they can proceed. If there is no user by that name in the custom table then the user is given an error message.

Ended up being a single line that was causing my issues but that can happen often when writing custom elements of any kind 🙂 Happy coding all.

Related Posts:

  1. How do I get WordPress login to ignore the password input if a particular username is used?
  2. Login in WordPress using external database data
  3. Finding the login page from database?
  4. Easiest Way to Authenticate WP Users Using an External DB?
  5. Who is responsible for data sanitization in WordPress development?
  6. What is the proper way to validate and sanitize JSON response from REST API?
  7. Why is it that not all users have a ‘session_tokens’ meta_key record in the usermeta table?
  8. Two websites one database, administrator not working on second website
  9. Finding the original login information in the database
  10. Database Table for customer user
  11. Auto login subdomain from main domain on single sites with different DB
  12. Logins from WP users table use on another page
  13. Is it possible to rebuild the website while not accesseing the original database?
  14. Using $wpdb | checking entered email against existing emails in db
  15. Recovering the table wp_usermeta from users
  16. Safely store code(html/js..) into database
  17. Unable to login to a https website via the app after changing from http
  18. How to protect my credentials on my clients website
  19. MySQL SELECT increment counter
  20. Exclude Statement in SQL
  21. Is it fine to have foreign key as primary key?
  22. Detecting errors generated by $wpdb->get_results()
  23. wpdb update add current timestamp not working
  24. Custom form that store input in database
  25. Restoring WordPress posts from database only
  26. Displaying content from one WP site on separate WP site
  27. Lost the `wp_options` table: what’s the best way to restore the site?
  28. Can a post ID be 0?
  29. Issue with wp_insert_post and post_content field error Could not update post in the database
  30. wp_posts – guid update
  31. Performance Gains of Relational DB Setup
  32. How to move existing WordPress wp-content folder along with database to new server and new domain name?
  33. what is the wp_5_posts table in the database?
  34. Migrating data between local and development server
  35. WordPress Database Charset/Collate
  36. store simple data in get_option()
  37. How might I retrieve a featured post image from an external WP site and display it as a link back?
  38. What is the database table for pages?
  39. Using Dynamic Data Pulled from a MySQL Table in a WordPress Page
  40. What are conventions about the schema of the $table_prefix
  41. WordPress database becoming huge. How to analyze and optimize it? fear of running out of memory
  42. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  43. BuddyPress: What’s the use of wp_bp_xprofile_data table and how does it get updated? [closed]
  44. storing database in version control
  45. Is deleting orphaned wp_options entries safe?
  46. How to build a movie database with wordpress?
  47. What’s the most efficient database method to add and query usermeta?
  48. How to solve slow WordPress site caused by attachment_metadata
  49. Is database safe after merging a branch of a more recent version over an older one?
  50. Sub-domain or 2nd, temporary different domain?
  51. Hook to be used when creating a database table
  52. Delete all post meta except featured image
  53. Where is custom template file chosen for a post stored in the DB?
  54. How do i get user data from a custom table in the wordpress database by user ID?
  55. Converting WordPress into Android or iPhone app
  56. Large database causes slow load
  57. Safe way to find last inserted id in a table?
  58. Delete duplicate rows from wordpress database where a column is duplicate in phpmyadmin
  59. Database slowdown after update to 3.4.1
  60. My custom page template with is_user_logged_in() does not detect that I’m logged in
  61. WooCommerce sku location in DB
  62. How to insert data into MySQL database from the form created in WordPress site
  63. Emoji support and MariaDB 10.0.30-
  64. WordPress DATABASE Update Manually?
  65. Is it possible to store visitors IPs in wp_postmeta table?
  66. I don’t see site_url and home_url fields in wp_options table (phpMyAdmin)?
  67. Multiple postmeta with same name for one post in wp_postmeta table
  68. Which is more efficient? Using usermeta, or creating a new MySQL table?
  69. Can I have multiple database users within WordPress?
  70. How do I copy a wordpress database?
  71. Export all content from wordpress
  72. Import live site to local setup without access to live site
  73. A field with dashes in the slug [closed]
  74. Creating a Video Content Page (how to use query strings in wordpress)- Help!
  75. Modify Database in Multi-Site wp_usermeta table
  76. Form Data not getting stored in WordPress database
  77. Hook directly into query execution
  78. Best way to tell if a user account is active, using the database only
  79. Structure of postmeta meta_value for woocommerce product download
  80. $wpdb doesn’t like to store arrays
  81. Connect to a remote database for wordpress in my own hosting server
  82. Handling large N data in WordPress
  83. How to replicate a user plugin made table value, to this user’s custom meta table value?
  84. Catalina an MAMP Database Conenction Error
  85. How can I force the user to log in, even if they’re already authenticated?
  86. WordPress ‘repairing database’ in a constant error loop?
  87. What Is the Proper Way to Merge Usermeta Table to a an existing WP Database?
  88. Importing CSV into database table not working?
  89. A MySQL DB within a MySQL DB
  90. WordPress returns to an old version
  91. data (html) migration to posts
  92. Super slow queries from author archive pages killing my database
  93. localize elements in database
  94. WordPress running SQL query to update database from form
  95. Check if an option exists and get its value in one hit
  96. WordPress url transformation script
  97. Just created a WordPress Table can’t get $wpdb get row to work – need help
  98. WordPress Database Query works in phpMyAdmin but not in the code
  99. Options table – where does my values go?
  100. How to use remote db tables in current config? [duplicate]
Categories Database Tags authentication, database, login, validation
Can’t log in. Log in button missing [closed]
How to add a column to edit posts area in admin panel?

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