1

I want to create a new way to Login.

What I got to do now:

  1. Edit the user form, adding a new field that is saved in user_meta
  2. Edit the Login form, adding a new input;
  3. Build a (safe) query that selects the user_id in usermeta table;

What I still need:

  1. Catch the post to login, run my query (3) and accept this as a valid user.

I could do this if I could overwrite the wp_signon function, but this is not possible.

Any other alternative?

3
  • There are no code. My attempts so far have worked. But now I do not know how I can override the function wp_signin
    – Falci
    Jun 8 '14 at 23:05
  • Do you mean wp_signon?
    – Will
    Jun 22 '14 at 16:29
  • yes, wp_signon.
    – Falci
    Jun 22 '14 at 18:22
1
+50

You can use this function in functions.php of your current theme It will show an extra field on login form

###################Code starts here ###################
<?php

add_action('login_form', 'add_login_field');
function add_login_field()
{
?>    
  <p>
      <label for="user_pass">My Input<br>
      <input type="text" name="my_name" value=""/>
  </p>
<?php  
}

function do_anything($user) {
    //do stuff

   $userdata= $user = get_userdatabylogin($user);
   $user_id = $userdata->ID;

   $my_name = $_POST['my_name'];



   add_user_meta( $user_id, 'my_name', $my_name);

}

add_action('wp_login', 'do_anything');
?>
############## CODE ENDS HERE ###################

The above code will create an extra field on the login form . This value will be posted with other values that can be received in "do_anything($user)" function. Finally this value can be saved as User meta.

Part 2=======================================================================

add_action('login_form', 'add_login_field');
function add_login_field()
{

?>    
       <p>
  <label for="user_pass">My Input<br>
  <input type="text" name="my_name" value=""/>
 </p>


 <?     
    }

function do_anything($user) {

    //do stuff

   $userdata= $user = get_userdatabylogin($user);
   $user_id = $userdata->ID;

   $my_name = $_POST['my_name'];


add_user_meta( $user_id, 'my_name', $my_name);
}
add_action('wp_login', 'do_anything');


add_filter('authenticate', 'check_login', 10, 3);

function check_login($user, $username, $password) {
  global $wpdb;  
if(isset($_POST['my_name']) && trim($_POST['my_name'])!="")
{
    $customfield    =   $_POST['my_name'];
$UsermetaData = $wpdb->get_results("SELECT *FROM  wp_usermeta WHERE meta_value='$customfield'");
$user = get_user_by('id', $UsermetaData[0]->user_id );

// Redirect URL //

if ( !is_wp_error( $user ) )
{
    wp_clear_auth_cookie();
    wp_set_current_user ( $user->ID );
    wp_set_auth_cookie  ( $user->ID );

    $redirect_to = user_admin_url();
    wp_safe_redirect( $redirect_to );
    exit();
}
return $user;
}

}

Please check this code snippet. Also you need to modify the code as per requirements. Make sure "my_name" field remain unique and it will not be updated every time on log in.

4
  • Thanks, but as I said in item 2, I managed to do this. How can I do to create a WP session knowing only the field "my_name" (user name and password became optional)
    – Falci
    Jun 25 '14 at 12:11
  • Whenever we add any user meta then it always need a user id and the key for that data to save in the database. The above functions will work only when we enter a valid username and password. Now the functionality you want "my_name" value need to be unique w.r.t. which we will get to know which user id need to login. Jun 25 '14 at 15:52
  • Updated the answer. Jun 26 '14 at 6:54
  • Welcome @Falci. Thanks for adding the reference in your code Jul 2 '14 at 7:02
0

If you want to catch the input before the login completes, I would think you might just hook the wp_authenticate action and run your script there.

"This action is located inside of wp_signon. In contrast to the wp_login action, it is executed before the WordPress authentication process."

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.