Authenticating users with usermeta fields

Ok seems like you have a few moving parts here…

  1. You want to add a new meta field (unique code) to the user meta for all users

  2. You want to modify the default login form by adding an extra field… a unique code field

  3. This unique code field should allow the user to login if they have provided the same unique code in the login form


  1. I’m going to assume you’ve added this meta, if not, I suggest using carbon-fields to achieve this

    Container::make( 'user_meta', 'Login Meta' )
    ->add_fields( array(
        Field::make( 'text', 'unique_code', 'Unique Code' ),
    ));
    

  1. To hook into the login form you’ll want to make use of the login_form action

    add_action( 'login_form', function(){
      echo '<input type="text" name="unique_code" placeholder="Enter your unique code here" />';
    });
    

  1. Authenticating with this unique code field

you’ll then want to look at the authenticate filter which you can hook into to force authenticate the user based on this posted “unique_code” field

 add_filter( 'authenticate', function( $user, $username, $password ){

      //do the check for the unique_code field here, 
      //which should be available in the $_POST object


     return $user;
  });

hope this helps, cheers