0

I'm trying to validate the form users submit when updating their profile data. I've hooked into user_profile_update_errors. It prints errors correctly to the user, however, it still allows the errors to be applied to the User's profile. The errors displayed are not actually enforced.

How can I prevent erred profile entries from being saved and instead block them?

Thanks

 function tml_profile_errors( $errors ) {
   if ( empty( $_POST['state'] ) )
       $errors->add( 'empty_missing_', '<strong>ERROR</strong>: Please enter your state.' );

   return $errors;
 }
 add_filter( 'user_profile_update_errors', 'tml_profile_errors' );

1 Answer 1

1

Solution:

First, Jeff Farthing of theme-my-login pointed out that I was using add_filter instead of add_action, and helped to craft the first code below, however it still has the same problem, so this is a step in the right direction but is not my solution:

function tml_profile_errors( &$errors ) {
    if ( empty( $_POST['state'] ) )
        $errors->add( 'empty_missing_', '<strong>ERROR</strong>: Please enter your state.' );
}
add_action( 'user_profile_update_errors', 'tml_profile_errors' );

Second, I added the same validation step inside of my 'personal_options_update' and 'edit_user_profile_update' actions using the code below. Since this is run during the save step, it will prevent the user from saving and invalid value. There are a lot more fields than just 'state' for the form, which is why the example below uses a nested if rather than an && operator:

function tml_edit_user_profile_update( $user_id ) {
     if ( current_user_can('edit_user',$user_id) ) {
         if ( !empty( $_POST['state'] ) )
           update_user_meta( $user_id, 'state', $_POST['state'] );
     }
 }
add_action('personal_options_update', 'tml_edit_user_profile_update');
add_action('edit_user_profile_update', 'tml_edit_user_profile_update');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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