最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom registration fields not validating

programmeradmin1浏览0评论

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
Add a comment  | 

3 Answers 3

Reset to default 1

the 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.

发布评论

评论列表(0)

  1. 暂无评论