The problem is that I have already a really "weird" coding on this form, due to the problem that Toolset require an email to save the form, but we want to be able to submit the form without it.
I have already a long thread open
https://toolset.com/forums/topic/email-not-required/
Finally, I was able to achieve what we wanted, and it works.
User can submit the form with/without email, and we automatically fill in the missing details.
// make the email field in a user form not required
add_filter('cred_filter_field_before_add_to_form', 'tssupp_not_required_email_func', 10, 1);
function tssupp_not_required_email_func($field){
if(isset($field['id']) && in_array($field['id'], array('user_email'))){
// in some cases $fields['data'] is an empty string, so you'll need to first set it's expected format for PHP 7.1 compatibility
if (!is_array($field['data'])) {
$field['data'] = array();
}
unset($field['data']['validate']['required']);
}
return $field;
}
// create a new user, so add a timestamp as login name, to avoid form errors
add_action('cred_before_save_data', 'contact_autogen_username',10,1);
function contact_autogen_username($form_data) {
// if a specific form
$forms = array( 5466,1574 );
if ( in_array( $form_data['id'], $forms ) )
{
$_POST['user_login'] = date('YmdHms'); // generate unique username based on current timestamp
}
}
// now set a proper username
add_action('cred_save_data','func_proper_username',12,2);
function func_proper_username ($user_id,$form_data) {
$forms = array( 5466, 1574 );
if ( in_array( $form_data['id'], $forms ) ) {
global $wpdb;
$username = strtolower($_REQUEST['first_name'] . $_REQUEST['last_name']) . $user_id ;
$wpdb->update($wpdb->users, array('user_login' => $username), array('ID' => $user_id));
}
}
// update the other names and add a customer number.
add_action('cred_submit_complete','func_create_new_customer',12,2);
function func_create_new_customer($user_id,$form_data) {
// if is a new created user
$forms = array( 5466, 1574 );
if ( in_array( $form_data['id'], $forms ) ) {
$first_name = ucwords(strtolower(sanitize_text_field($_REQUEST['first_name'])));
$last_name = ucwords(strtolower(sanitize_text_field($_REQUEST['last_name'])));
$username = strtolower( $first_name . $last_name ) . $user_id ;
$userdata = array (
'ID' => $user_id,
'first_name' => $first_name,
'last_name' => $last_name,
'nickname' => $first_name . " " . $last_name,
'user_nicename' => $username,
'display_name' => $first_name . " " . $last_name
);
wp_update_user($userdata);
update_user_meta( $user_id, 'wpcf-user-number', "KV-" . $user_id);
}
// change role
$forms = array( 5466, 1574, 9077 );
if ( in_array( $form_data['id'], $forms ) ) {
if ( isset ($_REQUEST['customer_select_role'])) {
$role = $_REQUEST['customer_select_role'];
}else{
$role = 'customer';
}
$user_meta = get_userdata($user_id);
$user_role = $user_meta->roles;
$u = new WP_User( $user_id );
$u->remove_role( $user_role ); //cred form roles field
$u->set_role( $role );
}
}
As you see, I need the form to go through the full process before I create the new user.
Now I need to find a way to use the "cred_form_validate" hook, as you suggested, to fire the function that create the custom post:
User X submit form -> create user X with the above code -> start function "add_customer_inquiry" to create post with author X
User Y submit form -> create user Y with the above code -> start function "add_customer_inquiry" to create post with author Y
User X submit again the form -> bypass the above code but -> start function "add_customer_inquiry" to create post with author X