• Hello! I am using AC login to manage registration and login.
    I would like to insert two flags under the login, one to accept the terms and conditions of privacy, and one to request consent to send newsletters. How can I do? Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • @buddha12345,

    In my opinion, your request goes beyond the scope of this plugin.

    To assist you, however, simply add the code snippet provided below to your functions.php file using the plugin, Code Snippets. The code snippet provided below adds a checkbox, text, and corresponding error message to the Registration Form.

    To add another checkbox, text, and corresponding error message simply duplicate the applicable lines of code.

    Adjust the affected lines of code (e.g., href, styling, etc.) to suit your needs.

    If done correctly, your Registration Form will look like this.

    Hope this helps.

    Cheers!

    ——————————————

    // 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="height: 1rem; width: 1rem;" />
        <label for="foo_privacy_policy"><?php _e( 'I accept your <a href="/privacy/" rel="noopener noreferrer" target="_blank" style="font-weight:500!important;">Privacy Policy</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
    }

    Update:

    Replace above code snippet with the following one: (this updated code ensures the checkmark remains checked and visible after clicking the “Register” button)

    // Add the checkbox to registration form
    add_action( 'register_form', 'foo_add_privacy_policy_field' );
    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
    }
    
    // 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' );
      }
    }
    Plugin Author weblizar

    (@weblizar)

    Hi @generosus

    thank you for the efforts you put into this
    we will consider this suggestion and add a better solution in future updates.
    if have any other suggestions feel free to let us know we will try our best to implement them as possible.

    If you have any issues or questions feel free to contact us again.

    Thanks & Best Regards,
    Weblizar Support.

    Thread Starter buddha12345

    (@buddha12345)

    Hello! Very kind! Thank you very much indeed! I have implemented the code and created the checkbox. I managed to create only one, however, if I enter the code twice an error occurs.
    How can I duplicate this checkbox with two different texts?
    Thank you!!

    generosus

    (@generosus)

    @buddha12345,

    My pleasure. Glad it worked for you.

    To created a second checkbox, copy and paste the above into Code Snippets using a new function name.

    Specifically (and as an example), you could change the following function names from

    foo_add_privacy_policy_field to foo_add_privacy_policy_field_addon

    foo_privacy_policy_auth to foo_privacy_policy_auth_addon

    foo_privacy_policy_save to foo_privacy_policy_save_addon

    Also, Code Snippets always provides excellent messages (or clues) when there’s an error. Whatever error(s) you’re getting, you can look them up online or contact the developer of Code Snippets for assistance.

    Last, if satisfied with the above solutions, can you kindly close this topic as “Resolved”?

    Cheers!

    Thread Starter buddha12345

    (@buddha12345)

    Hi, I entered this code to add another checkbox but it doesn’t work:

    // Add the checkbox to registration form
    add_action( 'register_form', 'foo_add_privacy_policy_field' );
    function foo_add_privacy_policy_field_addon() {
      $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( 'Desidero iscrivermi alla mailing list <a href="/privacy/" rel="noopener noreferrer" target="_blank"><u>Aglaya Design</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_addon( $errors, $sanitized_user_login, $user_email ) {
      if ( !isset( $_POST['foo_privacy_policy'] ) ) :
        $errors->add( 'policy_error', "<strong>Errore</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_addon( $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' );
      }
    }

    I only see a checkbox: https://aglayadesign.com/wp-login.php?action=register

    How can I do?
    A thousand thanks!!

    generosus

    (@generosus)

    @buddha12345,

    You forgot to change the name at other locations. Carefully review the code.

    Details: https://ibb.co/288pnwm

    Give it another spin. If it doesn’t work, and since your request impacts WordPress’ login form, you may want to post your issue in Fixing WordPress forum.

    Cheers!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Privacy Policy & Newsletter flag’ is closed to new replies.