Require Age Confirmation When Registering
Adds a required "I am at least 16 years old" checkbox to the WordPress and WooCommerce registration forms, since many privacy laws do not allow children to consent on their own.
Opens WPIDE on
Code
// Minimum age shown in the checkbox label.
$wpide_min_age = 16;
$wpide_age_field = function () use ( $wpide_min_age ) {
printf(
'<p class="form-row"><label for="wpide-age-ok"><input type="checkbox" name="wpide_age_ok" id="wpide-age-ok" value="1" required> %s</label></p>',
esc_html( sprintf( 'I confirm that I am at least %d years old.', $wpide_min_age ) )
);
};
$wpide_age_check = function ( $errors ) use ( $wpide_min_age ) {
if ( empty( $_POST['wpide_age_ok'] ) && is_wp_error( $errors ) ) {
$errors->add( 'wpide_age', sprintf( '<strong>Error:</strong> You must be at least %d years old to register.', $wpide_min_age ) );
}
return $errors;
};
add_action( 'register_form', $wpide_age_field );
add_action( 'woocommerce_register_form', $wpide_age_field );
add_filter( 'registration_errors', $wpide_age_check, 20 );
add_filter( 'woocommerce_registration_errors', $wpide_age_check, 20 );
Notes
Set $wpide_min_age to the age required in your country (13 to 16 under GDPR, 13 under COPPA).
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.