Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save celticwebdesign/4de47b800908cca6113334bcc291229e to your computer and use it in GitHub Desktop.

Select an option

Save celticwebdesign/4de47b800908cca6113334bcc291229e to your computer and use it in GitHub Desktop.
// https://wordpress.stackexchange.com/questions/87261/checkboxes-in-registration-form
// REGISTRATION
add_action( 'register_form', 'signup_fields_wpse_87261' );
add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );
// PROFILE
add_action( 'show_user_profile', 'user_field_wpse_87261' );
add_action( 'personal_options_update', 'save_profile_fields_87261' );
// USER EDIT
add_action( 'edit_user_profile', 'user_field_wpse_87261' );
add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );
function signup_fields_wpse_87261() {
?>
<label>
We will NOT use your contact details for any future marketing/promotional activities unless you consent and opt-in by ticking this box;<br />
<input type="checkbox" name="consent_opt_in" id="consent_opt_in" />
Opt-in?
</label>
<br />
<br />
<hr />
<?php
}
function handle_signup_wpse_87261( $user_id, $data = null ) {
$feat_a = isset( $_POST['consent_opt_in'] ) ? $_POST['consent_opt_in'] : false;
if ( $feat_a ) {
add_user_meta( $user_id, 'consent_opt_in', $feat_a );
}
}
function user_field_wpse_87261( $user ) {
$feat_a = get_user_meta( $user->ID, 'consent_opt_in', true );
?>
<h3><?php _e('Marketing Consent'); ?></h3>
<table class="form-table">
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="consent_opt_in" id="consent_opt_in" %1$s />',
checked( $feat_a, 'on', false )
);
?>
<span class="description"><?php _e('Opt-in to marketing material?'); ?></span>
</label>
</td>
</tr>
</table>
<?php
}
function save_profile_fields_87261( $user_id ) {
$feat_a = isset( $_POST['consent_opt_in'] ) ? $_POST['consent_opt_in'] : false;
update_usermeta( $user_id, 'consent_opt_in', $feat_a );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment