Skip Navigation

[Resolved] Unique user field value form to prevent duplicate values

This support ticket is created 4 years, 1 month 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by shaunV 4 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1822803

When submitting a create user form, I'd like to create a function that would query all users in the db and check if there's a field with matching value and if so, return an error message. That way I can create a unique nickname for each user and prevent duplicates.

Is there any documentation that you are following?
https://toolset.com/forums/topic/is-it-possible-to-avoid-duplicate-value-submission-for-any-cpt-field/
hidden link

Not working:

/**
 * CRED custom validation to prevent duplicates
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {
 
   	$nickname = get_post_meta($_POST['nickname']);
	$unique_field = 'nickname';  // Edit
    $form_ids = array( 3990, 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' => 'nickname',
    		'meta_value' => '$nickname',
    		'meta_compare' => '=' 
			);
 
        $matching = new WP_User_Query( $args );
 
        if ( !empty( $matching ) ) {
 
            $errors[ $unique_field ] = 'Value already used';
        }
    }
 
    return array($fields,$errors);
}

Let me know if this is out of the scope of the support team...
Thank you!

#1823793

Hello and thank you for contacting the Toolset support.

Supporting custom code is, indeed, beyond the scope of the support forum, but we'll still try to give some guidance or some example of how to use our APIs. https://toolset.com/toolset-support-policy/

You are using this custom code for User Forms, right?
If that's the case, I don't see why the get_post_meta function is used on line 7? You should probably just take the value of $_POST:

$nickname = $_POST['nickname'];

And You will need to remove the single quotes on line 18, or use double quotes instead:

            'meta_value' => $nickname,

Line 8 can be omitted, unless you want to use the variable $unique_field in the code:

            'meta_key' => $unique_field,

I hope this helps. Let me know if you have further questions.

#1825219

My issue is resolved now. Thank you!

Working code:


/**
 * CRED custom validation to prevent duplicates CREATE FORM 
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions_registration' ,10,2);
function prevent_duplicate_submissions_registration( $error_fields, $form_data) {
	$nickname = $_POST['nickname']; // Gets value from form
	$unique_field = 'nickname';  // Meta key stored in db
	
    $form_ids = array( 69696969 );  // 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' => $nickname,
    		'meta_compare' => '=' ,
			);
	
        $matching = new WP_User_Query( $args );
		
		$results = $matching->get_results();
        if ( !empty( $results ) ) {
            $errors[ $unique_field ] =  $nickname . ' is already in use. Choose a different name.';
        }
    }
 
    return array($fields,$errors);
};

/**
 * CRED custom validation to prevent duplicates EDIT FORM - excludes current user from the query as it will find a matching nickname
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' ,10,2);
function prevent_duplicate_submissions( $error_fields, $form_data) {
	$current_user = get_current_user_id();
	$nickname = $_POST['nickname']; // Gets value from form
	$unique_field = 'nickname';  // Meta key stored in db
	
    $form_ids = array(  420420 );  // 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' => $nickname,
    		'meta_compare' => '=' ,
			'exclude' => $current_user
			);
	
        $matching = new WP_User_Query( $args );
		
		$results = $matching->get_results();
        if ( !empty( $results ) ) {
            $errors[ $unique_field ] =  $nickname . ' is already in use. Choose a different name.';
        }
    }
 
    return array($fields,$errors);
};