Skip to content

Instantly share code, notes, and snippets.

@moafzalmulla
Created April 4, 2016 10:35
Show Gist options
  • Save moafzalmulla/a26ae87a7b4b3dbef523ce792e57b43f to your computer and use it in GitHub Desktop.
Save moafzalmulla/a26ae87a7b4b3dbef523ce792e57b43f to your computer and use it in GitHub Desktop.
Gravity forms 2 login forms with form id's of 17 and 20 . When functions are duplicated site breaks. When class is changed of one form, to avoid class clash still doesnt work. Duplicated forms below.
// ++++++++++++ 1st form - id 17 ++++++++++++++
// the _3 prefix has to match the id of the form you have created
add_action( "gform_after_submission_17", "login_form_after_submission", 10, 2 );
function login_form_after_submission($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_17", "login_validate_field", 10, 4 );
function login_validate_field($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;
}
// ++++++++++++ Second form : id 20 ++++++++++++++
// the _3 prefix has to match the id of the form you have created
add_action( "gform_after_submission_20", "login_form_after_submission", 10, 2 );
function login_form_after_submission($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", 10, 4 );
function login_validate_field($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'] === 'usernamelogin' ) {
$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'] === 'passwordlogin' ) {
if ( !$user or !wp_check_password( $value, $user->data->user_pass, $user->ID ) ) {
$result["is_valid"] = false;
$result["message"] = "Invalid password provided.";
}
}
return $result;
}
@moafzalmulla
Copy link
Author

This worked, when i did it for both functions :

// 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'] === 'usernamelogin' ) {
$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'] === 'passwordlogin' ) {
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