• Resolved aaron0574

    (@aaron0574)


    I am creating a website that has an age restriction for logged in users. That being said, I added the birthdate field to the registration form. I was curious if there was a settings configuration for the birthdate field so that an error is thrown if the user’s birthday is < 18 years from the date of registration.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aaron0574

    You can try this code snippets in your theme’s functions.php file or use the Code Snippets plugin to run the code:

    add_action("um_submit_form_register","um_062821_18_years_old");
    function um_062821_18_years_old( $post_form ){
      
        if(  isset( $post_form['birth_date'] ) && ! empty( $post_form['birth_date'] ) ){
            // register the error notice
            // $then will first be a string-date
            $then = strtotime( $post_form['birth_date'] );
            //The age to be over, over +18
            $min = strtotime('+18 years', $then);
            if(time() < $min)  {
                UM()->form()->add_error('birth_date', __( 'You should be over 17 years old.', 'ultimate-member' ) );
    		}
    	
        }  
    
    }

    The above code will validate the birth date to check if the age is over 17 years old or not. You should use the Birth Date field with the field key birth_date to make the above code work.

    Regards,

    Thread Starter aaron0574

    (@aaron0574)

    That worked like a charm. Thank you!

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aaron0574

    Thanks for letting us know. I’m marking this as resolved now.

    Regards,

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Restricting registration on age’ is closed to new replies.