admin管理员组文章数量:1325510
Hi I have created two custom fields - both of them should be required, but when I fill out username and correct email, the error messages are skipped and user gets registered. If there is an error in username or email, it writes the messages correctly. Could anybody give me a helping hand?
//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;
}
Hi I have created two custom fields - both of them should be required, but when I fill out username and correct email, the error messages are skipped and user gets registered. If there is an error in username or email, it writes the messages correctly. Could anybody give me a helping hand?
//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;
}
Share
Improve this question
asked Mar 2, 2016 at 17:24
Karolína VyskočilováKarolína Vyskočilová
4291 gold badge8 silver badges23 bronze badges
3 Answers
Reset to default 1the value of the checkbox wouldn't be "checked" - it should equal whatever you put in the "value" attribute .. and i think you need an extra set of parentheses in the if statement for first_name
to make sure your filter is firing you could try explicitly adding an error (with no validation checks) to see if you can force registration to fail
//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;
}
I've found my answer - unfortunatelly I've forgotten to mention that I'm using at same time the plugin New User Approve and that's what has been causing the problem of accepting before firing errors. Finally, thanks to @locomo I have google the right thing.
Answer found here: https://wordpress/support/topic/registration_errors-filter-problem
The new user is created in send_approval_email(). This function is called using the register_post action hook. Unfortunately, register_post fires before registration_errors so there is no way to validate data.
The solution:
replace this code (adding the validation):
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
with following
add_filter( 'register_post', 'myplugin_registration_errors', 9, 3 );
two changes have been made: registration_errors
become register_post
and 10
has been changed to 9
to have it fired at the right time.
@kybernaut.cz is right but there is an additional third change to be made.
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email )
Replace with
function myplugin_registration_errors( $sanitized_user_login, $user_email, $errors )
Then you would have no problem at all.
Remember to change $errors
variable position.
本文标签: Custom registration fields not validating
版权声明:本文标题:Custom registration fields not validating 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742196938a2431252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论