Advertisement
Guest User

Untitled

a guest
Dec 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. add_filter( 'authenticate', 'my_auth', 10, 3 );
  2.  
  3. function my_auth( $user, $username, $password ){
  4. return $user;
  5. }
  6.  
  7. function my_auth($user, $username, $password)
  8. {
  9. // Make sure a username and password are present for us to work with
  10. if ($username == '' || $password == '')
  11. return;
  12.  
  13. /* my_api_section
  14. * your API code goes here
  15. * Assuming it returns 0 for username and password missmatch
  16. * and 1 for match; store the result in $ext_auth['result']
  17. */
  18.  
  19.  
  20. if ($ext_auth['result'] == 0) {
  21. // User does not exist, send back an error message
  22. $user = new WP_Error('denied', __("ERROR: Username/password missmatch or not exists"));
  23.  
  24. } else if ($ext_auth['result'] == 1) {
  25. // External user exists, try to load the user info from the WordPress user table
  26. $userobj = new WP_User();
  27. $user = $userobj->get_data_by('email', $ext_auth['email']); // Does not return a WP_User object
  28. $user = new WP_User($user->ID); // Attempt to load up the user with that ID
  29.  
  30. if ($user->ID == 0) {
  31. // The user does not currently exist in the WordPress user table.
  32. // So creating the user.
  33. // Setup the minimum required user information for this example
  34. $userdata = array(
  35. 'user_email' => $ext_auth['email'],
  36. 'user_login' => $ext_auth['email'],
  37. 'first_name' => $ext_auth['first_name'],
  38. 'last_name' => $ext_auth['last_name']
  39. );
  40. $new_user_id = wp_insert_user($userdata); // A new user has been created
  41.  
  42. // Load the new user info
  43. $user = new WP_User($new_user_id);
  44. }
  45.  
  46. }
  47.  
  48. return $user;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement