您好,我创建了两个自定义字段-这两个字段都是必需的,但当我填写用户名和正确的电子邮件时,将跳过错误消息并注册用户。如果用户名或电子邮件中有错误,则会正确写入消息。谁能帮我一把吗?
//1. Add a new form element...
add_action( \'register_form\', \'myplugin_register_form\' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST[\'first_name\'] ) ) ? trim( $_POST[\'first_name\'] ) : \'\';
?>
<p>
<label for="first_name"><?php _e( \'Jméno a příjmení\', \'faveaplus\' ) ?><br />
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label>
</p>
<p>
<label for="subscribe">
<input type="checkbox" name="subscribe" value="1" />
Prohlašuji, že jsem pracovník ve zdravotnictví s oprávněním předepisovat nebo vydávat humánní léčivé přípravky (lékař nebo farmaceut).
</label>
</p>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( \'registration_errors\', \'myplugin_registration_errors\', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST[\'first_name\'] ) || ! empty( $_POST[\'first_name\'] ) && trim( $_POST[\'first_name\'] ) == \'\' ) {
$errors->add( \'first_name_error\', __( \'<strong>CHYBA</strong>: Musíte uvést své jméno a příjmení.\', \'faveaplus\' ) );
}
if ($_POST[\'subscribe\'] != "checked") {
$errors->add( \'subscribe_error\', __( \'<strong>CHYBA</strong>: Musíte prohlásit, že jste pracovník ve zdravotnictví\', \'faveaplus\' ) );
}
return $errors;
}
最合适的回答,由SO网友:Karolína Vyskočilová 整理而成
我找到了我的答案——不幸的是,我忘了提到我正在使用at same time the plugin New User Approve 这就是导致在发射前接受错误的原因。最后,多亏了@locomo,我用谷歌找到了正确的东西。
在此处找到答案:https://wordpress.org/support/topic/registration_errors-filter-problem
新用户是在send\\u approval\\u email()中创建的。使用register\\u post-action挂钩调用此函数。不幸的是,register\\u post在register\\u错误之前触发,因此无法验证数据。
The solution:
替换此代码(添加验证):
add_filter( \'registration_errors\', \'myplugin_registration_errors\', 10, 3 );
具有以下内容
add_filter( \'register_post\', \'myplugin_registration_errors\', 9, 3 );
进行了两项更改:
registration_errors 成为
register_post 和
10 已更改为
9 在正确的时间开火。
SO网友:locomo
复选框的值不会被“选中”-它应该等于您在“value”属性中输入的值。。我认为在if语句中,first\\u name需要一组额外的括号
要确保您的筛选器正在启动,您可以尝试显式添加错误(无需验证检查),以查看是否可以强制注册失败
//2. Add validation. In this case, we make sure first_name is required.
add_filter( \'registration_errors\', \'myplugin_registration_errors\', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST[\'first_name\'] ) || ( ! empty( $_POST[\'first_name\'] ) && trim( $_POST[\'first_name\'] ) == \'\' ) ) {
$errors->add( \'first_name_error\', __( \'<strong>CHYBA</strong>: Musíte uvést své jméno a příjmení.\', \'faveaplus\' ) );
}
if ( empty($_POST[\'subscribe\']) ) {
$errors->add( \'subscribe_error\', __( \'<strong>CHYBA</strong>: Musíte prohlásit, že jste pracovník ve zdravotnictví\', \'faveaplus\' ) );
}
return $errors;
}
SO网友:Sven Möller
@kybernaut。cz是对的,但还有第三个变化要做。
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email )
替换为
function myplugin_registration_errors( $sanitized_user_login, $user_email, $errors )
那你就没有问题了。记住要改变
$errors 可变位置。