Last active
May 21, 2016 13:41
-
-
Save moafzalmulla/5ce3413c8055b8d28c526390f1b1afc4 to your computer and use it in GitHub Desktop.
This code must sit is functions.php if you need a GF login form with he parameters username & password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You must set the css classes of both input fields to username and password respectively for this to work | |
// Code forums - cant remember exactly link | |
// the _3 prefix has to match the id of the form you have created | |
add_action( "gform_after_submission_20", "login_form_after_submission_20", 10, 2 ); | |
function login_form_after_submission_20($entry, $form) { | |
// get the username and pass | |
$username = $entry[1]; | |
$pass = $entry[2]; | |
$creds = array(); | |
// create the credentials array | |
$creds['user_login'] = $username; | |
$creds['user_password'] = $pass; | |
// sign in the user and set him as the logged in user | |
$sign = wp_signon( $creds ); | |
wp_set_current_user( $sign->ID ); | |
} | |
// the _3 prefix has to match the id of the form you have created | |
add_filter( "gform_field_validation_20", "login_validate_field_20", 10, 4 ); | |
function login_validate_field_20($result, $value, $form, $field) { | |
// make sure this variable is global | |
// this function is fired via recurrence for each field, s | |
global $user; | |
// validate username | |
if ( $field['cssClass'] === 'username' ) { | |
$user = get_user_by( 'login', $value ); | |
if ( empty( $user->user_login ) ) { | |
$result["is_valid"] = false; | |
$result["message"] = "Invalid username provided."; | |
} | |
} | |
// validate pass | |
if ( $field['cssClass'] === 'password' ) { | |
if ( !$user or !wp_check_password( $value, $user->data->user_pass, $user->ID ) ) { | |
$result["is_valid"] = false; | |
$result["message"] = "Invalid password provided."; | |
} | |
} | |
return $result; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment