Reputation: 11
I have utilised the provided code from ultimate member, trying to add a passcode validation in the Registration Form:
add_action("um_submit_form_register","um_122221_submit_form_register");
function um_122221_submit_form_register( $post_form ){
// Trigger error when the Passcode field is empty
if( isset( $post_form['user_passcode'] ) && empty( $post_form['user_passcode'] ) ){
UM()->form()->add_error('user_passcode', __( 'Passcode field can\'t be blank.', 'ultimate-member' ) );
}
// Trigger error when the Passcode entered is incorrect
if( isset( $post_form['user_passcode'] ) && ! empty( $post_form['user_passcode'] ) && "Banana" !== $post_form['user_passcode'] ){
UM()->form()->add_error('user_passcode', __( 'Passcode is incorrect.', 'ultimate-member' ) );
}
}
The above is entered in my Theme File functions.php page.
What happens is that if the user enters all the forms details correctly, but incorrectly enters the pass code, no error is shown and the user is registered and sent to the their new account page.
However, if the user enters all the forms details but makes another incorrect entry, such as they leave their email blank, the form is rendered back to the screen and the 'Passcode is incorrect' error message is displayed.
It seems that the trigger for when the passcode is incorrect is only being fired when another error is in the list.
Not sure if anyone else has had this issue ? and I have no idea how to get around this. Any ideas what might be wrong ?
For reference, I am using ultimate member version 2.8.6
Upvotes: 1
Views: 70
Reputation: 603
We need to add an explicit check
at the beginning of our function to ensure it processes the passcode validation
regardless of other errors. Here is the updated code which needs to be used.
<?php
add_action( 'um_submit_form_register', 'um_122221_submit_form_register' );
function um_122221_submit_form_register( $post_form ){
// Here we are checking for empty passcode.
if ( empty( $post_form['user_passcode'] ) ) {
UM()->form()->add_error( 'user_passcode', __( 'Passcode field can\'t be blank.', 'ultimate-member' ) );
}
// Here we are checking for incorrect passcode only if it's not empty.
if ( ! empty( $post_form['user_passcode']) && 'Banana' !== $post_form['user_passcode'] ) {
UM()->form()->add_error( 'user_passcode', __( 'Passcode is incorrect.', 'ultimate-member' ) );
}
// This code logic is a last resort to ensure all validations run.
if ( UM()->form()->has_errors() ) {
return;
}
}
Upvotes: 0