Last active
May 25, 2023 16:54
-
-
Save coulterpeterson/2d8cfbae77fd93bdd0744d4db4545daf to your computer and use it in GitHub Desktop.
Update User Password After Submission | Gravity Forms
This file contains hidden or 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
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Update User Password After Submission | |
* https://gravitywiz.com/ | |
* | |
* Updates the password for the user ID associated with the inputted email, allowing a custom password reset form. | |
* | |
*/ | |
// Update "123" to your form ID, "4" to your "Password" field ID, and "5" to your "Confirm Password" field ID. | |
// If you're not using a "Confirm Password" field, change "$double_password_confirmation" from "true" to "false". | |
add_filter( 'gform_field_validation_123_5', function( $result, $value, $form ) { | |
$double_password_confirmation = true; | |
if( ! $double_password_confirmation ) { | |
return; | |
} | |
$master = rgpost( 'input_4' ); | |
if ( $result['is_valid'] && $value != $master ) { | |
$result['is_valid'] = false; | |
$result['message'] = 'Passwords do not match. Please try again.'; | |
} | |
return $result; | |
}, 10, 4 ); | |
// Update "123" to your form ID, "4" to your "Email" field ID, and "5) to your "Password" field ID. | |
// If you're using GP Nested Forms, set these to the Form and Field IDs of the child form that contains the email and password fields. | |
// If you're using GP Populate Anything, make sure the "value" for the user email field is set to "User Email". | |
// If you're searching for users by their username instead of email, change $find_user_by_email to "false". | |
add_action( 'gform_after_submission_123', function ( $entry, $form ) { | |
$find_user_by_email = true; | |
$email_field_id = 4; | |
$password_field_id = 5; | |
$submission_email = rgar( $entry, $email_field_id ); | |
$submission_password = rgar( $entry, $password_field_id ); | |
if (strpos(strval($submission_email), '@') === false) { | |
// No @ found; must be the ID of a multi-select field instead | |
$field_check = RGFormsModel::get_field( $form, $email_field_id ); | |
$submission_email = is_object( $field_check ) ? $field_check->get_value_export( $entry, $email_field_id, true ) : false; | |
if( ! $submission_email ) { | |
return; | |
} | |
} | |
if( ! $find_user_by_email ) { | |
$user = get_user_by( 'login', $submission_email ); | |
} else { | |
$user = get_user_by( 'email', $submission_email ); | |
} | |
$user_id = $user->ID; | |
wp_set_password( $submission_password, $user_id ); | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment