Restrict registration for under 18 years old

Welcome mkmcst community Forum Web programming WordPress Ultimate Member Code Examples Restrict registration for under 18 years old

Viewing 0 reply threads
  • Author
    Posts
    • #2632
      michael mc
      Keymaster
      @michael

      This code snippet restricts registration for under 18 years old using the custom validation applied to the Birth Date field.

      Just add this code to the functions.php file in the active theme directory.

      <?php
      
      /**
       * Restrict registration for under 18 years old.
       *
       * @param array $args Form Arguments.
       */
      function um_custom_validate_birth_date( $args ) {
      	if ( ! empty( $args['birth_date'] ) ) {
      		// Birth date as a Unix timestamp.
      		$then = strtotime( $args['birth_date'] );
      
      		// A person 18'th birthday as a Unix timestamp.
      		$adulthood = strtotime( '+18 years', $then );
      
      		// Current time.
      		$now = time();
      
      		if ( $now < $adulthood ) {
      			UM()->form()->add_error( 'birth_date', __( 'You should be over 17 years old.', 'ultimate-member' ) );
      		}
      	}
      }
      add_action( 'um_submit_form_errors_hook_', 'um_custom_validate_birth_date', 30, 1 );

      https://docs.ultimatemember.com/article/1781-restrict-registration-for-under-18-years-old

Viewing 0 reply threads
  • You must be logged in to reply to this topic.