I'm trying to register a new user (with subscriber role) upon CRED form submission, if the email provided in the CRED form is not already registered in the user database.
Current CRED form has 2 custom fields which i intend to match with USER registration requirements.
Is there any documentation that you are following?
I'm think since the current CRED form already takes care of creating the post , my only issue is
1. creating the user if it doesn't already exist,
2. if the user already exist, simply prompt him/her to login and resubmit the CRED form
Looking for some directions here, please help point me to these topics is they have been previously discussed.
given that the solutions discussed was almost 5yrs ago, do let me know if there are more recent discussions, or that the suggested solutions is timeproof?
I can have some more references before setting it in action.
If you still need help, I will require admin access details and tell me where exactly you want to validate the user with what form.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Could you please share link where you added your post form and as I understand we need to check the input email with existing user email, if email is found within users we should show error otherwise create a new user - right?
Thank you for looking at this.
The post form is at hidden link
we need to check the input email with existing user email, if email is found within users we should show error otherwise create a new user - right?
Ans: that is correct, if email is found within users, i think the proper thing to do is to ask prompt them to login and resubmit the form. of course if the data on the form can be retained while he/she logins, it will be best practice.
I've added the following Toolset form validation hook to "Custom Code" section offered by Toolset with the code snippet namely "custom-validate-user":
=> hidden link
add_filter('cred_form_validate','func_validate_email_with_domain',10,2);
function func_validate_email_with_domain($error_fields, $form_data){
//field data are field values and errors
list($fields,$errors)=$error_fields;
if ($form_data['id']==15701){
$the_user = get_user_by('email',trim($_POST['wpcf-contact-email']));
if(!empty($the_user)){
$errors['wpcf-contact-email']='This E-mail already exists and registered. Please try different E-Mail.';
}
}
return array($fields,$errors);
}
I can see now when there is an email exists, it will throw the error as described above:
This E-mail already exists and registered. Please try different E-Mail.
You can change the error message above as required.
Thank you.
I think I might have some errors in my codes.
I tried submitting a post form on the front end but no new user is registered.
Able to share where I may have gone wrong?
that works.. I have to remind myself that custom fields have wpcf- prefix.
Lets say I want the user to be created before cred save, I'll use the cred_before_save_data hook?
I'm asking because Im gonna try making the created user the author of the submitted post.
It looks like the present flow the user is created after data save. am i right?
edited line 23 to include the missing '$'
I've also added a line to send notification email to admin and new user if user creation is successful.
Thank you for your help Minesh!
add_action('cred_save_data', 'func_user_creation',10,2);
function func_user_creation($post_id, $form_data){
// if a specific form
if ($form_data['id']==XXXXX) {
if (isset($_POST['wpcf-xxxxx'])){
$website = "xxxxx.com";
$userdata = array (
'user_login'=> $_POST['wpcf-xxxxx'],
'user_email'=> $_POST['wpcf-xxxxx'],
'user_url' => $website,
'user_pass'=> wp_generate_password()
);