Adding a Checkbox for GDPR Compliance
Adding a checkbox can be achieved in three simple steps, or two if you don't need to save the fact that the checkbox was checked.
Add the Checkbox
function add_gdpr_checkbox_to_tml_register_form() {
tml_add_form_field( 'register', 'accept_terms', array(
'type' => 'checkbox',
'label' => 'I agree to the <a href="/terms-and-conditions">Terms and Conditions</a>',
'value' => '1',
'checked' => tml_get_request_value( 'accept_terms', 'post' ),
'priority' => 30,
) );
}
add_action( 'init', 'add_gdpr_checkbox_to_tml_register_form' );
Validate the Checkbox
function validate_gdpr_checkbox_on_registration( $errors ) {
if ( ! tml_get_request_value( 'accept_terms', 'post' ) ) {
$errors->add( 'accept_terms', '<strong>ERROR</strong>: You must accept the Terms and Conditions.' );
}
return $errors;
}
add_filter( 'registration_errors', 'validate_gdpr_checkbox_on_registration' );
Save the Checkbox (Optional)
function save_gdpr_checkbox_on_registration( $user_id ) {
if ( tml_get_request_value( 'accept_terms', 'post' ) ) {
update_user_meta( $user_id, 'accepted_terms', 1 );
}
}
add_action( 'user_register', 'save_gdpr_checkbox_on_registration' );