Hi,
I was looking something like this :
https://toolset.com/forums/topic/is-it-possible-to-avoid-duplicate-value-submission-for-any-cpt-field/
But, instead in CPT, I want it in User Forms that I created, do you can help me achieve it?
The idea is I want to avoid the same value for phone number when user register in my site, so when there is same value in that field there will be a error message when user submit the form
This is the code from above link for your reference :
/**
* CRED custom validation to prevent duplicates
*/
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {
$post_type = 'cpt-slug'; // Edit
$unique_field = 'wpcf-' . 'field-slug'; // Edit
$form_ids = array( 101, 115 ); // Edit IDs of CRED forms
list( $fields,$errors ) = $error_fields;
if ( in_array($form_data['id'], $form_ids ) ) {
// Get existing posts with unique field
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'numberposts' => -1,
'meta_key' => $unique_field,
'meta_value' => $fields[ $unique_field ]
);
$matching = get_posts( $args );
if ( !empty( $matching ) ) {
$errors[ $unique_field ] = 'Value already used';
}
}
return array($fields,$errors);
Thanks
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Darma,
Thank you for getting in touch.
Is it that the code is not working for you ? Or you have not tried it as yet.
If so then you need to use the code below for users.
/**
* CRED custom validation to prevent duplicates
*/
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {
$unique_field = 'wpcf-' . 'field-slug'; // Edit
$form_ids = array( 101, 115 ); // Edit IDs of CRED forms
list( $fields,$errors ) = $error_fields;
if ( in_array($form_data['id'], $form_ids ) ) {
// Get existing posts with unique field
$args = array(
'meta_key' => $unique_field,
'meta_value' => $fields[ $unique_field ],
'meta_compare' => '=',
);
$matching = get_users( $args );
if ( !empty( $matching ) ) {
$errors[ $unique_field ] = 'Value already used';
}
}
return array($fields,$errors);
Replace the 'field-slug' with the slug of your field as well as remove 101 and 115 and add the ID of your form here.
Please try this and let me know if this helps.
Thanks,
Shane
Hi Shane,
Thanks for your prompt reply, but after I implement the code above into my function.php, it resulted to be something like in the attached image.
The code that I paste in the function.php is something like this :
/**
* CRED custom validation to prevent duplicates
*/
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {
$unique_field = 'wpcf-' . 'nomor-telepon-user'; // Edit
$form_ids = array( 67, 117 ); // Edit IDs of CRED forms
list( $fields,$errors ) = $error_fields;
if ( in_array($form_data['id'], $form_ids ) ) {
// Get existing posts with unique field
$args = array(
'meta_key' => $unique_field,
'meta_value' => $fields[ $unique_field ],
'meta_compare' => '=',
);
$matching = get_users( $args );
if ( !empty( $matching ) ) {
$errors[ $unique_field ] = 'Value already used';
}
}
return array($fields,$errors);
}
"nomor-telepon-user" is the text field that I want to avoid duplication and "67" & "117" is Form ID
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Darmaj,
I see the issue, change this line here from :
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
To
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' ,10,2);
This should resolve the issue.
Thanks,
Shane
Hi Shane,
thanks for your reply, yes it already resolve the submission issue, but when I test it still can register the user that have the same phone number, do you know what went wrong?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Darmaj,
Would you mind providing me with admin access to the site so that I can see exactly why this isn't working for you ?
The private fields will be enabled for your next response.
Also please let me know the page where the form was added.
Thanks,
Shane
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Darmaj,
I've fixed it for the new user form.
So if you try to create a new user with an already existing number the error will be thrown.
Thanks,
Shane
Hi Shane,
Thanks it already worked as I expected!
My issue is resolved now. Thank you!