Help Appreciated | Checkmark Inside Checkbox Not Saving on Registration Form
-
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)
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.