1

I am using the "authenticate" filter hook within the WordPress codex to adjust the validation script of the WordPress Login page.

What I need to do is create a query to a separate table to see if there is a row in the database or not for the individual logging in.

Here is what I have so far.

 add_filter( 'authenticate', 'check_user_is_in_ibew_members', 40, 3 );
 function check_user_is_in_ibew_members( $user, $username, $password ) {
    global $wpdb;
    $Card = $username
    $result = $wpdb->get_results( "SELECT 1 FROM members WHERE Card = $Card LIMIT 1");
   // $query = "SELECT 1 FROM members WHERE Card = $Card LIMIT 1";
   // $result = $wpdb->get_results($query);
   foreach ($result as $query) {
        // Evaluates to true because $query is empty
        if (empty($query)) {
            echo 'Card Number is currently inactive';
        }

    }
    return $user;
 }

Basically when the user tries to log in we select 1 row from the table where Card is equal to the WP username. If the member is in the database the form should return true and log the visitor in. If the visitor is not in the table then the form should return false and provide an error message.

It is not seeming to work. I have checked and there are no errors in the code itself which makes me believe I am implementing it incorrectly.

Can i get a bit of assistance as to what I have done wrong so that I can learn from my mistakes. I thank all who assist.

  • You can check $result->RPT much easier and safer by usind filter_var(), as shown here. This also saves you the ! is_null() check. – kaiser Jun 13 '17 at 5:47
  • Hi @kaiser thanks for the tip, I will look into that regarding the rpt function, the current issue I am having though is trying to verify a table row exists. I have updated my code to better reflect my question in hopes that I can get a bit of assistance on this topic. – DigitalDesigner Jun 13 '17 at 5:58
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.