Terms and Privacy Checkbox on Registration
Adds a required "I agree to the terms and privacy policy" checkbox to the WordPress registration form and records when each user agreed.
Opens WPIDE on
Code
// URL of your terms page (the privacy policy page is linked automatically when set).
$wpide_terms_url = home_url( '/terms/' );
add_action( 'register_form', function () use ( $wpide_terms_url ) {
$privacy = get_privacy_policy_url();
?>
<p style="margin-bottom:16px">
<label>
<input type="checkbox" name="wpide_accept_terms" value="1" <?php checked( ! empty( $_POST['wpide_accept_terms'] ) ); ?>>
I agree to the <a href="<?php echo esc_url( $wpide_terms_url ); ?>" target="_blank" rel="noopener">terms of use</a>
<?php if ( $privacy ) : ?>and the <a href="<?php echo esc_url( $privacy ); ?>" target="_blank" rel="noopener">privacy policy</a><?php endif; ?>.
</label>
</p>
<?php
} );
add_filter( 'registration_errors', function ( $errors ) {
if ( empty( $_POST['wpide_accept_terms'] ) ) {
$errors->add( 'wpide_terms', '<strong>Error:</strong> Please accept the terms to create an account.' );
}
return $errors;
} );
add_action( 'user_register', function ( $user_id ) {
if ( ! empty( $_POST['wpide_accept_terms'] ) ) {
update_user_meta( $user_id, 'wpide_terms_accepted', current_time( 'mysql' ) );
}
} );
Notes
Covers the default wp-login.php registration form; shop or membership plugins have their own forms.
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.