Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. // ++++++++++++ 1st form - id 17 ++++++++++++++
  2. // the _3 prefix has to match the id of the form you have created
  3. add_action( "gform_after_submission_17", "login_form_after_submission", 10, 2 );
  4. function login_form_after_submission($entry, $form) {
  5. // get the username and pass
  6. $username = $entry[1];
  7. $pass = $entry[2];
  8. $creds = array();
  9. // create the credentials array
  10. $creds['user_login'] = $username;
  11. $creds['user_password'] = $pass;
  12. // sign in the user and set him as the logged in user
  13. $sign = wp_signon( $creds );
  14. wp_set_current_user( $sign->ID );
  15. }
  16.  
  17.  
  18. // the _3 prefix has to match the id of the form you have created
  19. add_filter( "gform_field_validation_17", "login_validate_field", 10, 4 );
  20. function login_validate_field($result, $value, $form, $field) {
  21. // make sure this variable is global
  22. // this function is fired via recurrence for each field, s
  23. global $user;
  24. // validate username
  25. if ( $field['cssClass'] === 'username' ) {
  26. $user = get_user_by( 'login', $value );
  27. if ( empty( $user->user_login ) ) {
  28. $result["is_valid"] = false;
  29. $result["message"] = "Invalid username provided.";
  30. }
  31. }
  32. // validate pass
  33. if ( $field['cssClass'] === 'password' ) {
  34. if ( !$user or !wp_check_password( $value, $user->data->user_pass, $user->ID ) ) {
  35. $result["is_valid"] = false;
  36. $result["message"] = "Invalid password provided.";
  37. }
  38. }
  39. return $result;
  40. }
  41.  
  42.  
  43. // ++++++++++++ Second form : id 20 ++++++++++++++
  44.  
  45.  
  46. // the _3 prefix has to match the id of the form you have created
  47. add_action( "gform_after_submission_20", "login_form_after_submission", 10, 2 );
  48. function login_form_after_submission($entry, $form) {
  49. // get the username and pass
  50. $username = $entry[1];
  51. $pass = $entry[2];
  52. $creds = array();
  53. // create the credentials array
  54. $creds['user_login'] = $username;
  55. $creds['user_password'] = $pass;
  56. // sign in the user and set him as the logged in user
  57. $sign = wp_signon( $creds );
  58. wp_set_current_user( $sign->ID );
  59. }
  60.  
  61.  
  62. // the _3 prefix has to match the id of the form you have created
  63. add_filter( "gform_field_validation_20", "login_validate_field", 10, 4 );
  64. function login_validate_field($result, $value, $form, $field) {
  65. // make sure this variable is global
  66. // this function is fired via recurrence for each field, s
  67. global $user;
  68. // validate username
  69. if ( $field['cssClass'] === 'usernamelogin' ) {
  70. $user = get_user_by( 'login', $value );
  71. if ( empty( $user->user_login ) ) {
  72. $result["is_valid"] = false;
  73. $result["message"] = "Invalid username provided.";
  74. }
  75. }
  76. // validate pass
  77. if ( $field['cssClass'] === 'passwordlogin' ) {
  78. if ( !$user or !wp_check_password( $value, $user->data->user_pass, $user->ID ) ) {
  79. $result["is_valid"] = false;
  80. $result["message"] = "Invalid password provided.";
  81. }
  82. }
  83. return $result;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement