PHP WordPress注册表格必填复选框

问题描述 投票:0回答:1

我正在尝试使我的WordPress注册表单符合GDPR的要求,用户需要选中一个复选框以接受我们的隐私政策。

我已经通过使用钩子register_form成功完成了此复选框。

但是,我在使复选框字段为必填项时遇到问题。我在数组中尝试了必选true,但似乎无法正常工作。我可以解释一下为了使此复选框成为必需框而可以做什么吗?

add_action( 'register_form', 'login_extra_note' );

function login_extra_note() {

woocommerce_form_field( 'privacy_policy_reg', array(
   'type'          => 'checkbox',
   'class'         => array('form-row privacy'),
   'label_class'   => array('label-for-checkbox checkbox'),
   'input_class'   => array('form__input-checkbox input-checkbox'),
   'required'      => true,
   'label'         => 'Jeg har læst og acceptere <a href="https://example.dk/privatliv/" target="blank">Betingelser & Privatlivspolitik</a>',
));

}
php wordpress checkbox required
1个回答
0
投票

该问题与没有错误消息关联到chekbox字段有关。我设法通过在复选框字段中添加错误来解决它:

// add registration chekbox field
add_action( 'register_form', 'login_extra_note' );

function login_extra_note() {

woocommerce_form_field( 'privacy_policy_reg', array(
   'type'          => 'checkbox',
   'class'         => array('form-row privacy'),
   'label_class'   => array('label-for-checkbox checkbox'),
   'input_class'   => array('form__input-checkbox input-checkbox'),
   'required'      => true,
   'label'         => 'Jeg har læst og acceptere <a href="https://example.dk/privatliv/" target="blank">Betingelser & Privatlivspolitik</a>',
));

}

// Error registration form
function my_registrationform_error( $errors, $sanitized_user_login, $user_email ) {

    if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
    $errors->add( 'demo_error', __( '<strong>FEJL</strong>: Du skal accepter VIFT Betingelser & Privatlivspolitik!', 'my_textdomain' ) );
        }
    return $errors;
}

add_filter( 'registration_errors', 'my_registrationform_error', 10, 3 );
© www.soinside.com 2019 - 2024. All rights reserved.