$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - Block Update Profile Errors|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

hooks - Block Update Profile Errors

matteradmin7PV0评论

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' );

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' );
Share Improve this question asked Sep 4, 2015 at 17:38 qna101qna101 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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');

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far