Site icon WPBrigade

Add Privacy Policy Checkbox in Registration Form

Privacy Policy

By default, the user registration for WordPress is off. Once you enable it, you’ll find it doesn’t let you gather much information from users when they first register on your site. It has basic fields, including Username or Email Address and Password

Custom fields empower you to get extra information from your users, such as an address or phone number, or ask them to accept your Terms & Conditions, Privacy Policy, etc.

Important: The WordPress Checkboxes or checkboxes at any website are legal, as clicking on a checkbox is urging a signature on a written contract.

Here we’ll explain how to add the custom field “Privacy Policy” in the WordPress registration form with custom coding. So, make sure you read this article till the end.

Let’s get started!

Enable WordPress Registration Form

Note: We assume you’ve already enabled the WordPress registration form.

If not, simply go to the left sidebar of the admin dashboard, navigate to Settings, and click the General option.

The General Settings screen will open up. Scroll down a bit, and tick mark the Membership checkbox. 

When you are done, scroll done and click on the Save Changes button to save the settings. 

Now you can see the registration link on your login page:

Adding Custom Field  “Privacy Policy” on the Registration Form 

Here are going to add the custom “Privacy Policy” checkbox to the user registration form in WordPress. This process will be carried out using custom coding, so buckle up!

1. Hook for Adding Privacy Policy Field on the Registration Form

Note: register_form Allow rendering of new HTML element.

Paste the following code in the child theme’s functions.php file to add the Privacy Policy checkbox on the registration form.

// Add privacy policy field.
add_action( 'register_form', 'loginpress_add_privacy_policy_field' );
function loginpress_add_privacy_policy_field() { ?>
  <p>
    <label for="lp_privacy_policy"><?php _e( 'Privacy Policy', 'loginpress' ) ?><br />
      <input type="checkbox" name="lp_privacy_policy" id="lp_privacy_policy" class="checkbox" />
    </label>
  </p>
  <?php
}

2. Hook for Validating Privacy Policy Field on the Registration Form

Note: registration_errors Perform validation on form registration fields.

You’ll need to add the following code snippet in the child theme’s functions.php file to make sure each user won’t get registered to your site before tick marking the Privacy Policy checkbox.

// Add validation. In this case, we make sure lp_privacy_policy is required.
add_filter( 'registration_errors', 'loginpresss_privacy_policy_auth', 10, 3 );

function loginpresss_privacy_policy_auth( $errors, $sanitized_user_login, $user_email ) {

  if ( ! isset( $_POST['lp_privacy_policy'] ) ) :

    $errors->add( 'policy_error', "<strong>ERROR</strong>: Please accept the privacy policy." );
    return $errors;
  endif;
  return $errors;
}

3. Hook for Updating User Meta on the Registration Form

Note: user_register  Save custom form data.You are almost done! Paste the following code in the child theme’s functions.php file to save custom form data.

// Lastly, save our extra registration user meta.
add_action( 'user_register', 'loginpress_privacy_policy_save' );

function loginpress_privacy_policy_save( $user_id ) {

  if ( isset( $_POST['lp_privacy_policy'] ) )
     update_user_meta( $user_id, 'lp_privacy_policy', $_POST['lp_privacy_policy'] );
}

All done! We hope you understand how to add the custom field “Privacy Policy” in the WordPress registration form with custom coding. Now it’s your turn!

Let us know how much this knowledgebase article helped you! If you have any doubts or questions related to this matter, please don’t hesitate to contact our support team.

Exit mobile version