Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gkarthikeyanmca/dc4bb1c2ec8b54f6f8d164a8134f4c54 to your computer and use it in GitHub Desktop.
Save gkarthikeyanmca/dc4bb1c2ec8b54f6f8d164a8134f4c54 to your computer and use it in GitHub Desktop.
<?php
/* Formidable forms hook to validate form entries dynamically */
add_filter('frm_validate_field_entry', 'validate_user_credentials', 10, 3);
function validate_user_credentials( $errors, $field, $posted_value ){
if ( $field->id == 11 ) {
/* Check if user entered username already exists. If yes then we show an error message */
if ( username_exists( $posted_value ) ) {
//Error message if username already exists
$errors['field' . $field->id] = 'Username "'.$posted_value.'" already exists. Please try a different username.';
}
}
if ( $field->id == 8 ) {
/* Check if user entered email already exists. If yes then we show an error message */
if ( email_exists( $posted_value ) ) {
//Error message if email already exists
$errors['field' . $field->id] = 'Email "'.$posted_value.'" already exists. Please try a different email.';
}
}
return $errors;
}
?>
<?php
/* Formidable hook which will be triggered when user submits the form */
add_action('frm_after_create_entry', 'mysite_create_user', 30, 2);
function mysite_create_user($entry_id, $form_id){
if($form_id == 2){ //Check if form id 2, that is my registration form
//User data from form response.
$args = array(
'user_login' => $_POST['item_meta'][11], //11 is the field ID of Username field in form
'user_email' => $_POST['item_meta'][8], //8 is the field ID of Email field in form
'first_name' => $_POST['item_meta'][6], //6 is the field ID of First name field in form
'last_name' => $_POST['item_meta'][7], //7 is the field ID of Last name field in form
'role' => 'subscriber' // Set role as subscriber OR you can set to any other WP roles you want
);
//Create user
$user_id = wp_insert_user( $args );
/*
Store address in usermeta.
This is an example of how to get extra fields during user registration and store them in usermeta
You are free to add any additional fields in registration form and store them in usermeta
*/
update_user_meta($user_id,'user_address',$_POST['item_meta'][10]); //10 is the field ID of Address field in form
/* Send password reset link for the new user. We send */
send_password_reset_email_new_user($_POST['item_meta'][11]); //11 is the field ID of Username in form
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment