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.

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
#1899899

OK. I have created a post form OMREN Registration in post type OMRENS, (Check the test site). I have added 3 fields (Name and email ) are in the Post fields group and Civil ID under the User Field group. I don't know how to add the Civil Id field in the Post form. I can add only the two fields Name and Email.

#1899987

From what I see, you have created the Civil ID as a user field, so, you can't add it to a Post Form. Instead, you can add it to a User Form.

Before going further, I suggest that we agree first on what is to be done. Can you explain why would you add the field to a Post Form? Can you elaborate more on what you intend to build?

#1899991

I did not want to create a User form. We want all the fields in Post form. I have created Civil ID on the User Fields as per your advice before. I think it should be in the post fields. The idea is that I don't want the user to register two times using the same civil ID

#1900081

The idea is that I don't want the user to register two times using the same civil ID
Users are created using User Forms. Posts are created using Post Forms.

The form that you have created in the test site is for creating new posts in the OMRENS post type hidden link

If you want to register new users, you need to create a User form. The Civil ID should be a user field, instead of a post field. Unless you want to have ONE post in OMRENS per Civil ID, in that case, we can manage it using a post field.

Check chapter 5 in our tutorial for building a membership site. hidden link
https://toolset.com/course/wordpress-membership-sites/

As soon as you build the User Form I'll check it and test the custom code to make the Civil ID unique.

#1900761

No, I don't want a user form. I want only a post form. I don't want the users to have accounts on our website. I want them to post their details only.

#1901893

Any update on this issue ?..

#1902095

As you don't need to create users with unique Civil ID and what you are looking for is creating OMREN posts with unique Civil ID. I was able to prepare it on the test site with the following steps:
- Added a custom field Civil ID (with slug cid) to the post fields group.
- Added the field to the form.
- Added the following code as a custom code snippet in Toolset settings and activated it. hidden link

add_filter( 'cred_form_validate', 'uni_identifier' ,10,2);
function uni_identifier( $error_fields, $form_data) {
  $unique_field = 'cid'; // Toolset uses the prefix 'wpcf-' at the database level
  $cid = $_POST['wpcf-' . $unique_field]; // Gets value from form
  $form_ids = array( 15 ); // 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' => 'wpcf-' . $unique_field,
      'meta_value' => $cid,
      'meta_compare' => '=' ,
    );
    $posts = get_posts( array( 'meta_query' => $args ) );
    if ( count($posts) > 0 ) {
      $errors[ $unique_field ] = $cid . ' is already in use.';
    }
  }
  
  return array($fields,$errors);
};

Check this screenshot from my test hidden link

#1903499

I have tried it on your test site. When I enter any Number it shows (Already Exist).

I have also tried the code in my form. Same, but without an error message I am not able to click submit, the form is not loading.

#1906185

Make sure that you use the correct field slug at line 3 and the correct form(s) IDs at line 5. If it still does not work, take a Duplicator copy, or an All In One Migration export and let me download it and work on it. Please exclude the uploads folder to reduce the size of the copy.
- https://wordpress.org/plugins/duplicator/
- https://wordpress.org/plugins/all-in-one-wp-migration/
Your next reply will be private to let you share the download link safely.

Let me know what form we are talking about and in what URL I can see it.

#1907959

The code is correct. But the form does not include the form messages element. You need to add the following shortcode to the form:

[cred_field field='form_messages' class='alert alert-warning']

Check this screenshot hidden link

Then make sure that the custom code snippet is active. Check this screenshot hidden link

#1911559

Hi Jamal,

Sorry for the late reply, We have changed the idea of the form to give the user account in our application. The scenario will be now :
the user will access using his Civil ID and card reader. and a hidden page should check from DB before access the application page.
If the user does not exist --> redirect him to the registration page.
if the user exists --> login him directly and redirects him to his profile.

How can I do that?

New threads created by Jamal and linked to this one are listed below:

https://toolset.com/forums/topic/authenticate-the-user-or-offer-registration/

#1914111

For support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. For that reason, I have created another ticket to help you with this last request. I'll be posting my reply there.
Please mark this ticket as resolved.