Last active
July 15, 2020 10:36
-
-
Save gkarthikeyanmca/dc4bb1c2ec8b54f6f8d164a8134f4c54 to your computer and use it in GitHub Desktop.
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
<?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; | |
} | |
?> |
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
<?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 | |
} | |
} | |
?> |
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
<?php | |
/* | |
Function to send password reset link to WordPress user | |
Parameter should be WordPress username | |
*/ | |
function send_password_reset_email_new_user($user_login = "") { | |
if($user_login != ""){ | |
//Get user object by username | |
$user = get_user_by('login',$user_login); | |
//Get password reset key | |
$key = get_password_reset_key( $user ); | |
//Start composing email message for user | |
$message = __('Hello ') . $user->first_name. " ".$user->last_name.",\r\n\r\n"; | |
$message .= __('Your account has been created.') . "\r\n\r\n"; | |
$message .= home_url( '/' ) . "\r\n\r\n"; | |
$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; | |
$message .= __('Reset your password using following link.') . "\r\n\r\n"; | |
//Add password reset link in message | |
$message .= wp_login_url() . "?action=rp&key=".$key."&login=".rawurlencode($user_login)."\r\n\r\n"; | |
$message .= __('Many thanks') . "\r\n"; | |
//Check if multisite and get blogname accordingly | |
if ( is_multisite() ) | |
$blogname = $GLOBALS['current_site']->site_name; | |
else | |
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); | |
//Set email title/subject | |
$title = sprintf( __('[%s] Password Reset'), $blogname ); | |
$title = apply_filters('retrieve_password_title', $title); | |
//Check if email is sent successfully | |
if ( $message && !wp_mail($user->user_email, $title, $message) ){ | |
//wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') ); | |
echo "<p><span class='error-message'>Email not sent</span> Possible reason: your host may have disabled the mail() function. Please contact administrator.</p>"; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment