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;
}
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;
}
  • Thanks a lot, but it didn't help. I have tried to add only error without condition and it ends the same way, user get registered. – kybernaut.cz Mar 3 '16 at 21:53
  • I have find it finally - I've forgotten to say, that I'm usin New User Approve plugin, and as you asked me about firing an error I started to look for registration_error not working and I found it here, it fires before the registration_error and it's necessary to change it to register posts – kybernaut.cz Mar 3 '16 at 22:10
  • great, glad you figured it out! – locomo Mar 4 '16 at 14:40
0

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.org/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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.