Skip Navigation

[Resolved] Registering a new user (subscriber) upon CRED form submission..

This support ticket is created 2 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by davelow 2 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2386965

Tell us what you are trying to do?

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 was looking at the following interesting example, but realise it was doing it the other way round (i.e. in USER form, create a custom post)
https://toolset.com/forums/topic/how-to-combine-a-user-form-with-a-post-form-in-one-submit/
https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/

I had tried to look for some other examples in the toolset support forum and have this as reference:
https://toolset.com/forums/topic/create-user-account-with-cred-post-form-informations/

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.

Thank you in advance.

Is there a similar example that we can see?

What is the link to your site?

#2387035

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To validate the form to check for existing user, you will require to use the Toolset Form's hook cred_form_validate.

Please check the following ticket that should help you to understand how you can check for duplicate email within the post. You are welcome to change the code as required as per your convenience:
- https://toolset.com/forums/topic/split-how-to-validate-specific-email-domain-with-forms/#post-1203558

To create the user, with the following ticket, there is already a sample code, again you can adjust the code as required:
- https://toolset.com/forums/topic/create-user-account-with-cred-post-form-informations/#post-348645

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.

#2387237

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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?

#2387517

Hi Minesh,

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.

Thanks Minesh

#2388953

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2388957

Hi Minesh,

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?

Dave

#2388975

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please try to delete the existing code you are using and try to use the following code:

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']==15701) {
        if (isset($_POST['wpcf-owner'])){
            $website = "dogsactually.com";
            $userdata = array (
                'user_login'=> $_POST['wpcf-owner'],
                'user_email'=> $_POST['wpcf-contact-email'],
                'user_url' => $website,
                'user_pass'=> wp_generate_password()
                );
            
            $user_id = wp_insert_user( $userdata );

            //On Success
            if (! is_wp_error( $user_id )){
                echo "User created: ". $user_id;
            }
        }
    }
}

Could you please try above code and check if that helps you to resolve the issue.

#2389115

Thanks Minesh.

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?

Thanks,
Dave

#2389117

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can still make the author of the current post.

Please try to use the following updated code where I've set the author of the post as newly created user.

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']==15701) {
        if (isset($_POST['wpcf-owner'])){
            $website = "dogsactually.com";
            $userdata = array (
                'user_login'=> $_POST['wpcf-owner'],
                'user_email'=> $_POST['wpcf-contact-email'],
                'user_url' => $website,
                'user_pass'=> wp_generate_password()
                );
             
            $user_id = wp_insert_user( $userdata );
 
            //On Success
            if (! is_wp_error( $user_id )){
                echo "User created: ". $user_id;
            }

               $my_post = array(
                        'ID' => $post_id,
                        'post_author' => user_id
                        );
            // Update the post into the database
            wp_update_post( $my_post );


        }
    }
}
#2401615

My issue is resolved now. Thank you!

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()
);

$user_id = wp_insert_user( $userdata );

//On Success
if (! is_wp_error( $user_id )){
echo "User created: ". $user_id;
wp_new_user_notification( $user_id, null, 'both' );
}

$my_post = array(
'ID' => $post_id,
'post_author' => $user_id
);
// Update the post into the database
wp_update_post( $my_post );

}
}
}