Skip Navigation

[Resolved] Unique field hook

This support ticket is created 3 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 26 replies, has 2 voices.

Last updated by Jamal 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1887803

In our form, the user should enter his ID number, the ID should not be replicated it should be unique. I have found through my search that we have to use Hooks. But I don't know how to use hooks and where to place them.

Could you please assist me?

#1888087

Hello and thank you for contacting the Toolset support.

Exactly, this will need custom code to be done. The custom code can be added in different places. Either in the theme's functions.php file or another file, in a separate plugin, or in Toolset->Settings->Custom Code. Read about the latest option here:
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

I think that you are using a User registration form, right? In that case, take inspiration from this custom code https://toolset.com/forums/topic/phone-field-unique-in-the-user-registration/#post-1860755

If this is about a Post Form, check this one https://toolset.com/forums/topic/force-unique-field-on-cred-post/#post-621220

If you are unsure about it, allow me temporary access to your site and I'll see what I can do. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1889299

It is difficult to give you access since our website should be accessed through a VPN.

I have added the below code in the toolset custom code snippet. but it doesn't work. What is the issue?

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

/**
* CRED custom validation to prevent duplicates CREATE FORM
*/
add_filter( 'cred_form_validate', 'uni_identifier' ,10,2);
function uni_identifier( $error_fields, $form_data) {
$aid = $_POST['id-number1']; // Gets value from form
$unique_field = 'aid'; // Meta key stored in db

$form_ids = array( 7666 ); // 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' => $aid,
'meta_compare' => '=' ,
);

$matching = new WP_User_Query( $args );

$results = $matching->get_results();
if ( !empty( $results ) ) {
$errors[ $unique_field ] = $aid . ' is already in use.';
}
}

return array($fields,$errors);
};

#1889417

I have changed the code,

the new one is below. But still not working

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

/**
* CRED custom validation to prevent duplicates CREATE FORM
*/
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
// if it is the specific CRED form
if (isset($form_data['id']) && $form_data['id']!=485516) return $error_fields;

list($fields,$errors)=$error_fields;

//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
$r=$fields['wpcf-id-number1'];

$query = new WP_Query( $r );
if( $query->have_posts() ) {
// Error "CPF already saved" is not working
$errors['wpcf-id-number1']='ID already saved';
}
return array($fields,$errors);
}

#1889671

First, let's agree if this a User form, or a Post form. If it is a User Form, the second code is not correct. The first code may be correct, but it depends on what is the field slug. I am not sure if it is "id-number1" or "aid"?

Please note that WP_Query searches in the posts table and WP_User_Query searches in the users' table.

Let's say that this is about a User Form, that the number field was created by Toolset and its slug is "id-number1", and that the form ID is7666 , I believe the following code will work:

add_filter( 'cred_form_validate', 'uni_identifier' ,10,2);
function uni_identifier( $error_fields, $form_data) {
	$aid = $_POST['id-number1']; // Gets value from form
	$unique_field = 'wpcf-id-number1'; // Toolset uses the prefix 'wpcf-' at the database level
	
	$form_ids = array( 7666 ); // 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' => $aid,
			'meta_compare' => '=' ,
		);
		
		$matching = new WP_User_Query( $args );
		
		$results = $matching->get_results();
		if ( !empty( $results ) ) {
			$errors[ $unique_field ] = $aid . ' is already in use.';
		}
	}
	
	return array($fields,$errors);
};

If it does not work for you, I'll need to take a closer look at your form, the custom fields, and the custom code that you are using. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1890317

Thank you for your support,

Sorry, I forget to mention that it is a post form, not a user form.

The field slug is (id-number1) and form Id is 7666

I cannot share the credentials because the website accessed using VPN. But I can share a backup from the duplicator plugin.

#1892455

Because it is a Post from, we need to use WP_Query instead of WP_User_Query. The code becomes:

add_filter( 'cred_form_validate', 'uni_identifier' ,10,2);
function uni_identifier( $error_fields, $form_data) {
    $aid = $_POST['id-number1']; // Gets value from form
    $unique_field = 'wpcf-id-number1'; // Toolset uses the prefix 'wpcf-' at the database level
     
    $form_ids = array( 7666 ); // 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' => $aid,
            'meta_compare' => '=' ,
        );
         
        $matching = new WP_Query( $args );
         
        $results = $matching->get_results();
        if ( !empty( $results ) ) {
            $errors[ $unique_field ] = $aid . ' is already in use.';
        }
    }
     
    return array($fields,$errors);
};

I am setting your next reply as private to let you share a download link to the Duplicator backup, I'll build it locally and check it further. Please let me know what page or URL on the frontend where the form is being used. Please create an administrator user and share its credentials before taking the backup.

#1894741

I built the Duplicator copy in my local setup, and I found one User form "OMREN services" it has several generic fields such as:
- ID Number
- Date of Birth
- Mobile Number
- Other Number
- Email

I assume that you are looking to make the ID Number unique, right?
If yes, this field should not be a generic field. It has to be a user field. Go to Toolset->Custom Fields, then User Fields(tab), and create a new Group with the field on it. hidden link
Then, go to the form and update it to include this field instead of a generic field. Then, then adapt the custom code to reflect the actual slug of the field that you will be creating. Does it make sense?

#1894761
user field.JPG

Dear Jamal,
thank you for your support,

yes, ID should be unique.

I have tried to add the newly created user field in the form but it comes like on the attached image and it does not appear when I run the form.

#1894823

Would it be possible to access your live site or to work on a staging site? This way we will always be on the same version.

Otherwise, I can create a user field on my local copy and test it, but I won't be sure that we are having the same thing.

If you can't have a staging site, would you like to migrate your website to our platform? We'll work on it until we fix it then you can implement the same on your website.

#1896213

I have got VPN access to the website. Could you please open a private reply to send you the access details

#1898517

Of course. Your next reply will be private.

#1898813

I installed the FortiClient software but I am not able to configure it. I guess that I need an IP address or something else.
hidden link

Can you walk me through the configuration?

#1899447

please check the configuration hidden link

#1899503

Even after login into the VPN, I am still rejected by the site. Check this screenshot hidden link

I suggest that you create only the custom field, and the form on one of our test sites, so I can help you with that, then you can adapt the solution to your website. Use the following link to login in hidden link