admin管理员组文章数量:1394217
I need to add a privacy policy checkbox to user registration AND user profile (checkable and uncheckable at anytime in your user life). I was thinking there would be tons of plugins doing this, but I am searching for hours not finding anything suitable.
I could find how to add a checkbox programmatically to registration form (like this) but not on the user profile. So how this can be done?
I need to add a privacy policy checkbox to user registration AND user profile (checkable and uncheckable at anytime in your user life). I was thinking there would be tons of plugins doing this, but I am searching for hours not finding anything suitable.
I could find how to add a checkbox programmatically to registration form (like this) but not on the user profile. So how this can be done?
Share Improve this question asked May 29, 2018 at 15:24 Sasha GrievusSasha Grievus 3371 gold badge4 silver badges13 bronze badges3 Answers
Reset to default 3To add fields on user profile page,there are some filters for displaying them and some filters for saving them and another action for updating personal options.
The extended answer...
// 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="float: left; height: 50px;" />
<label for="foo_privacy_policy"><?php _e( 'Privacy Policy', '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>: Please accept the privacy policy." );
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="float: left; height: 50px;" <?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
}
add_action( 'woocommerce_register_form', 'add_registration_privacy_policy', 11 );
function add_registration_privacy_policy() {
woocommerce_form_field( 'privacy_policy_reg', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true,
'label' => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
));
}
add_filter( 'woocommerce_registration_errors', 'validate_privacy_registration', 10, 3 );
function validate_privacy_registration( $errors, $username, $email ) {
if ( ! is_checkout() ) {
if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
$errors->add( 'privacy_policy_reg_error', __( 'Privacy Policy consent is required!', 'woocommerce' ) );
}
}
return $errors;
}
本文标签: How to add a checkbox to registration and user profile
版权声明:本文标题:How to add a checkbox to registration and user profile? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744759398a2623662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论