Posted on Leave a comment

How to Verify MemberPress Age 18+ Years Old

If you need to verify that a Member’s age is greater than 18. Then use the script below.

I like pasting scripts like this into the free Code Snippets plugin. Paste it into the Code Snippets plugin and select “Run Everywhere” as the Snippet Type.

First you’ll need to create a new Birthday field in the MemberPress > Settings > Fields tab. After you create the field, it should have slug. Something like mepr_birthday . If your slug is different, you’ll need to replace it in the code below before saving the Code Snippet.

If you want to change the age, then you’ll need to change the +18 years part to a different age such as +13 years for example. You’d also need to change the 18 in the error message also.

Anyways, hope this helps someone out there!

// Validate if user is over 18 years old
add_filter('mepr-validate-signup', 'validate_birthday_input');
function validate_birthday_input($errors) {
  $slug = 'mepr_birthday'; // CHANGE THIS TO MATCH THE SLUG OF YOUR BIRTHDAY FIELD
  
  if(is_user_logged_in()) { return $errors; }
  
  $then = strtotime($_POST[$slug]);
  
  if(!isset($_POST[$slug]) || empty($_POST[$slug])) {
    $errors[] = "You must enter your birthday.";
  }
  
  $min = strtotime('+18 years', $then);
  
  if(time() < $min)  {
    $errors[] = "Your age is under 18 so you are not allowed to sign up"; 
  }
  return $errors;
}
Leave a Reply

Your email address will not be published. Required fields are marked *