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. WordPress Admin login problems- PLEASE HELP [closed]
  20. What is an ORM, how does it work, and how should I use one? [closed]
  21. What is the difference between an ORM and an ODM?
  22. What is this JavaScript “require”?
  23. Rename a table in MySQL
  24. How should I tackle –secure-file-priv in MySQL?
  25. What is this JavaScript “require”?
  26. What is “Advanced” SQL?
  27. What’s the difference between identifying and non-identifying relationships?
  28. Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
  29. Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
  30. Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
  31. What are the differences between B trees and B+ trees?
  32. MySQL SELECT increment counter
  33. How to resolve ORA-011033: ORACLE initialization or shutdown in progress
  34. Example of a strong and weak entity types
  35. Oracle SQL query for Date format
  36. phpMyAdmin – Error > Incorrect format parameter?
  37. Strange Characters in database text: Ã, Ã, ¢, â‚ €,
  38. Difference between partition key, composite key and clustering key in Cassandra?
  39. How SID is different from Service name in Oracle tnsnames.ora
  40. What is a relation in database terminology?
  41. Failed to connect to mysql at 127.0.0.1:3306 with user root access denied for user ‘root’@’localhost'(using password:YES)
  42. Database vs File system storage
  43. Can I have multiple primary keys in a single table?
  44. Exclude Statement in SQL
  45. How do you query for “is not null” in Mongo?
  46. What is a file based database?
  47. Is it fine to have foreign key as primary key?
  48. Storing Images in DB – Yea or Nay?
  49. Using wpdb to connect to a separate database
  50. how to Optimize WordPress database for 10 million post? [duplicate]
  51. MySQL Database User: Which Privileges are needed?
  52. Why is my database import losing text widget data?
  53. Can I programmatically login a user without a password?
  54. Database synchronization between dev/staging and production
  55. Keeping WP database synced across multiple developers using git
  56. WPDB Insert or if exists Update
  57. Multiple developers / editors working on a site in progress
  58. How can I make updates to a site, on a development copy, but then move updates back without overriding live site’s evolving database?
  59. get_results using wpdb
  60. Will it break my site if I delete all transient records in wp_options table?
  61. Checking if Database Table exists
  62. Why WordPress choose data serialization over json_encode?
  63. What’s the simplest way to backup my WordPress database?
  64. WordPress (MyISAM) database is slow, should I switch to InnoDB?
  65. $wpdb won’t insert NULL into table column
  66. Relaunch 4.2 utf8mb4 database upgrade
  67. Fastest way (least amount of steps) to locally import a remote database using WP-CLI
  68. Safest way to bulk delete post revisions
  69. Please explain how WordPress works with MySQL character set and collation at a low level
  70. Solution for database version control and deployment?
  71. The MySQL alternatives: Do Percona Server and MariaDB work well with WordPress, and do they make WordPress go better?
  72. How many users can WordPress handle?
  73. Get error messages when $wpdb->insert() returns false?
  74. Detecting errors generated by $wpdb->get_results()
  75. How to Change the Default Home Page for the WordPress Dashboard?
  76. Staging sites, how do you manage synchronising updates in the DB?
  77. Does dbDelta delete columns as well?
  78. How to get the post publish date outside the loop?
  79. MariaDB as a backend database
  80. wpdb update add current timestamp not working
  81. Site stuck in “Database Update Required” loop
  82. How to fetch Data in WordPress using MySQLi or $wpdb
  83. WordPress Database lost auto increment
  84. Count & Display Database Queries
  85. Connect to database using wordpress wp-config file
  86. How do I require authorization / login to view a specific set of posts / pages?
  87. What is $interim_login?
  88. How can I make a WordPress database portable and url independent?
  89. Are There Any Plans for WordPress to Support Databases Other Than MySQL?
  90. wpdb->insert multiple record at once
  91. How to migrate a HTTPS WordPress installation to localhost?
  92. Two WordPress sites sharing the same content
  93. How to integrate custom database table in WordPress and using WordPress functions
  94. Reset Post IDs to less than 64bit integer
  95. WP Multisite: load content from site X on site Y
  96. Adding extra authentication field in login page
  97. How To Export/Import WordPress (MySQL) Database Properly Via Command-Line?
  98. Replacing the WordPress password validation
  99. Custom form that store input in database
  100. Restoring WordPress posts from database only
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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