• Resolved generosus

    (@generosus)


    Good Day!

    I have added a Privacy Policy checkbox to our WordPress registration form.

    Everything works great, except the checkmark inside the checkbox is not saving after purposely (or inadvertenly) leaving either the Username or Email field blank and clicking the Register button. Click here for details.

    The checkbox was created by adding the code snippet provided below to our functions.php file.

    Any ideas on how to modify the code snippet to ensure the checkmark is saved after clicking the Register button?

    Thank you!

    —————————

    Add Privacy Policy Checkbox to WordPress Registration Form:

    // 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="margin-top: .2px; height: 1rem; width: 1rem;" />
        <label for="foo_privacy_policy" style="display: inline!important;"><?php _e( 'I have read and accept your <a href="/privacy/" rel="noopener noreferrer" target="_blank"><u>Privacy Policy</u></a>.', '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>: Privacy Policy not accepted." );
        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="height: 1rem; width: 1rem;" <?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
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Your <input type="checkbox" needs a value-attribute. Without value the condition for check its state never match.

    Thread Starter generosus

    (@generosus)

    Hi @threadi,

    How would I modify the snippet to accomplish what you recommended?

    Thank you!

    Change this line

    <input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" style="margin-top: .2px; height: 1rem; width: 1rem;" />

    to

    <input type="checkbox" name="foo_privacy_policy" value="1" id="foo_privacy_policy" class="checkbox" style="margin-top: .2px; height: 1rem; width: 1rem;" />

    Thread Starter generosus

    (@generosus)

    Hi @threadi,

    Thank you. Unfortunately, did not work.

    To understand better my issue, visit this registration form.

    Complete all fields except the Email field, then click “Register” You will notice the checkmark does not remain saved. It disappears. Does not remain visible as, for example, in the login form.

    Cheers!

    Jetzt verstehe ich was Du meinst. Ersetze deine Funktion ‘foo_add_privacy_policy_field‘ mal durch folgenden Code:

    function foo_add_privacy_policy_field() {
      $checked = !isset( $_POST['foo_privacy_policy'] ) ) ? ' checked="checked"' : '';
      ?>
      <p>
        <input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" value="1" <?php echo $checked; ? style="margin-top: .2px; height: 1rem; width: 1rem;" />
        <label for="foo_privacy_policy" style="display: inline!important;"><?php _e( 'I have read and accept your <a href="/privacy/" rel="noopener noreferrer" target="_blank"><u>Privacy Policy</u></a>.', 'foo' ) ?>
        </label>
      </p>
      <?php
    }

    Background: when the supplemented tick is output, its status must also be set in the output HTML code. For this purpose, PHP reads out whether the tick was set and, if so, sets the corresponding HTML attribute.

    Thread Starter generosus

    (@generosus)

    Vielen dank. Ihr code funktionierte, ben?tigte aber ein paar kleinere anpassungen. Hier ist der arbeitscode:

    function foo_add_privacy_policy_field() {
      $checked = isset( $_POST['foo_privacy_policy'] ) ? ' checked="checked"' : '';
      ?>
      <p>
        <input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" value="1" <?php echo $checked; ?> style="margin-top: .2px; height: 1rem; width: 1rem;" />
        <label for="foo_privacy_policy" style="display: inline!important;"><?php _e( 'I have read and accept your <a href="/privacy/" rel="noopener noreferrer" target="_blank"><u>Privacy Policy</u></a>.', 'foo' ) ?>
        </label>
      </p>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Help Appreciated | Checkmark Inside Checkbox Not Saving on Registration Form’ is closed to new replies.