Halloween Sale 15% off Ends in See the plans
PHP

Terms and Privacy Checkbox on Registration

Adds a required "I agree to the terms and privacy policy" checkbox to the WordPress registration form and records when each user agreed.

Don’t have WPIDE yet? Install it on your site

Code

// URL of your terms page (the privacy policy page is linked automatically when set).
$wpide_terms_url = home_url( '/terms/' );

add_action( 'register_form', function () use ( $wpide_terms_url ) {
    $privacy = get_privacy_policy_url();
    ?>
    <p style="margin-bottom:16px">
        <label>
            <input type="checkbox" name="wpide_accept_terms" value="1" <?php checked( ! empty( $_POST['wpide_accept_terms'] ) ); ?>>
            I agree to the <a href="<?php echo esc_url( $wpide_terms_url ); ?>" target="_blank" rel="noopener">terms of use</a>
            <?php if ( $privacy ) : ?>and the <a href="<?php echo esc_url( $privacy ); ?>" target="_blank" rel="noopener">privacy policy</a><?php endif; ?>.
        </label>
    </p>
    <?php
} );

add_filter( 'registration_errors', function ( $errors ) {
    if ( empty( $_POST['wpide_accept_terms'] ) ) {
        $errors->add( 'wpide_terms', '<strong>Error:</strong> Please accept the terms to create an account.' );
    }

    return $errors;
} );

add_action( 'user_register', function ( $user_id ) {
    if ( ! empty( $_POST['wpide_accept_terms'] ) ) {
        update_user_meta( $user_id, 'wpide_terms_accepted', current_time( 'mysql' ) );
    }
} );

Notes

Covers the default wp-login.php registration form; shop or membership plugins have their own forms.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.