1

I need to add a privacy policy checkbox to user registration AND user profile (checkable and uncheckable at anytime in your user life). I was thinking there would be tons of plugins doing this, but I am searching for hours not finding anything suitable.

I could find how to add a checkbox programmatically to registration form (like this) but not on the user profile. So how this can be done?

2

To add fields on user profile page,there are some filters for displaying them and some filters for saving them and another action for updating personal options.

  • Thank you, I add an extended version of the answer, just for people passing by, looking for code – Sasha Grievus May 29 '18 at 16:41
1

The extended answer...

// Add the checkbox to registration form
add_action( 'register_form', 'foo_add_privacy_policy_field' );
function foo_add_privacy_policy_field() { 
  ?>
  <p>
    <input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" style="float: left; height: 50px;" />
    <label for="foo_privacy_policy"><?php _e( 'Privacy Policy', 'foo' ) ?>
    </label>
  </p>
  <?php
}

// Validate the checkbox value in the registration form so that it is required
add_filter( 'registration_errors', 'foo_privacy_policy_auth', 10, 3 );
function foo_privacy_policy_auth( $errors, $sanitized_user_login, $user_email ) {
  if ( !isset( $_POST['foo_privacy_policy'] ) ) :
    $errors->add( 'policy_error', "<strong>ERROR</strong>: Please accept the privacy policy." );
    return $errors;
  endif;
  return $errors;
}

// Fill the meta 'foo_privacy_policy' with the value of the checkbox
add_action( 'personal_options_update', 'foo_privacy_policy_save' );
add_action( 'edit_user_profile_update', 'foo_privacy_policy_save' );
add_action( 'user_register', 'foo_privacy_policy_save' );
function foo_privacy_policy_save( $user_id ) {
  if ( isset( $_POST['foo_privacy_policy'] ) ){
    update_user_meta( $user_id, 'foo_privacy_policy', $_POST['foo_privacy_policy'] );
  }else{
    update_user_meta( $user_id, 'foo_privacy_policy', 'off' );
  }
}

// Add the checkbox to user profile home
add_action( 'show_user_profile', 'foo_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'foo_show_extra_profile_fields' );
function foo_show_extra_profile_fields( $user ) {
    ?>
    <h3><?php esc_html_e( 'Privacy Policy', 'foo' ); ?></h3>

    <table class="form-table">
        <tr>
            <td>
            <input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" style="float: left; height: 50px;" <?php if(get_the_author_meta('foo_privacy_policy', $user->ID)=='on' ){ echo "checked"; } ?> />
    <label for="foo_privacy_policy"><?php _e( 'Privacy Policy', 'foo' ) ?>
    </label></td>
        </tr>
    </table>
    <?php
}
0
add_action( 'woocommerce_register_form', 'add_registration_privacy_policy', 11 );
function add_registration_privacy_policy() {
    woocommerce_form_field( 'privacy_policy_reg', array(
        'type'          => 'checkbox',
        'class'         => array('form-row privacy'),
        'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
        'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
        'required'      => true,
        'label'         => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
    ));
}

add_filter( 'woocommerce_registration_errors', 'validate_privacy_registration', 10, 3 );
function validate_privacy_registration( $errors, $username, $email ) {
    if ( ! is_checkout() ) {
        if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
            $errors->add( 'privacy_policy_reg_error', __( 'Privacy Policy consent is required!', 'woocommerce' ) );
        }
    }
    return $errors;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.