Add a custom field for age verification 18 years and older

I made a birthday field in the register form of the user… I need help on where (in which files) can I put the check for the age, if it is 18 years and older, I have the implementation of this check, but I don’t know exactly where to put this to make the check when the user registers…

i added this code to make the field for the birthday in the register form inside user_register.php file:

'birthday' => [ 'label' => esc_html__( 'Birthday', 'hivepress' ),
                      'type' => 'date', // Use 'date' type for a date picker
                      'required' => true,
                      'max_length' => 64,
                      '_order' => 40,
                    ],  

I have the implementation for the check:
// Custom validation for the birthday field add_filter
( 'hivepress/v1/models/user/validate', 
   function( $errors, $fields, $values ) { 
             if ( isset( $values['birthday'] ) ) { 
 
            // Convert the birthday to a timestamp 
             $birthday = strtotime( $values['birthday'] );

             // Check if the date conversion failed 
             if ( !$birthday ) { 
                     $errors[] = esc_html__( 'Invalid birthday format.', 'hivepress' ); } 
             else {
                    // Calculate the age 
                    $age = ( time() - $birthday ) / ( 60 * 60 * 24 * 365.25 ); 
            
              // Check if the age is less than 18 
              if ( $age < 18 ) { 
                    $errors[] = esc_html__( 'You must be at least 18 years old.', 'hivepress' ); 
               } 
           } 
       } 
      return $errors; 
 }

I think this is not correct ‘hivepress/v1/models/user/validate’ and i need help on where to put this code to make the check when the user registers.

Thank you,
George Kyritsis.

Please try adding this field as a custom attribute in Users/Attributes. If you mark it as Editable and Required, it’ll appear in the registration form. Then you can use the hivepress/v1/models/user/validate action to check the value, e.g.:

$user->get_birth_date()

Replace “birth_date” with the attribute field name. The second argument for the hook callback is the $user object so you can use it this way and add an error to the filtered errors array and return it.

Hope this helps

Can you give me a more detailed solution because I cannot understand exactly everything, maybe an example with the code will help, because I am not familiar with PHP.

Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Sorry for the late reply, for some reason this topic wasn’t assigned to me on time.

I recommend adding a custom Date attribute in Users/Attributes and mark it Editable and Required (then it’ll appear in the registration form), e.g. name it Date of Birth. Then you can use hivepress/v1/forms/user_register/errors hook (for example) to check the entered value, and if the entered date is less than 18 years ago you can add a custom error. Here’s a similar code snippet for the same hook Restrict email domains for newly registered users #hivepress #users · GitHub