• Resolved mylesmarkevich

    (@mylesmarkevich)


    I have a custom user meta field where I want to accept only integers between 0 and 50. I’m using an input field of type=”number”, and I’m checking from within my update function if the input is any good or not. This is sort of working… If the input is no good, the value doesn’t get saved. But you still get a message saying “User Updated”. I would rather be able to return an error that explains the problem with the input. I’m having a lot of trouble figuring out how to do that… Can I do this from within my update function? Or is there a different way I’m supposed to do it?

    Here’s what I have so far:

    This is my input box:

    
    add_action('edit_user_profile', 'my_plugin_user_settings');
    function my_plugin_user_settings($user){
    ?>
    <table class="form-table">
      <tr>
        <th>
          <label for="my-plugin-user-level">User Level</label>
        </th>
        <td>
          <input type="number" name="my-plugin-user-level" value="<?php echo (get_user_meta($user->ID, 'my-plugin-user-level', true) == NULL) ? '0' : get_user_meta($user->ID, 'my-plugin-user-level', true); ?>">
        </td>
      </tr>
    </table>
    <?php
    }
    

    And here’s what I’m using to check and update the user meta:

    
    add_action('edit_user_profile_update', 'my_plugin_user_level_update');
    function my_plugin_user_level_update($user_id){
      if(!current_user_can('edit_user', $user_id)){
        return false;
      }
      $value = intval($_POST['my-plugin-user-level']);
      if(!is_int($value) || $value < 0 || $value > 50){
        return false; // I want to do some kind of error message here instead
      }
      return update_user_meta($user_id, 'my-plugin-user-level', $value);
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @mylesmarkevich ,

    You will need to use ‘user_profile_update_errors ‘ filter hook:

    So what you will need to do is, whatever validation you have written in the ‘my_plugin_user_level_update’, you will need to shift that code in the callback of ‘user_profile_update_errors’ filter hook.

    Please refer the snippet below:

    
    function validate_profile_form_fields( $errors ) {
    	if ( empty( $_POST['company'] ) ) {
    		$errors->add( 'empty_company', '<strong>ERROR</strong>: Please enter your company name.' );
    	}
    	return $errors;
    }
    add_filter( 'user_profile_update_errors', 'validate_profile_form_fields' );
    
    Thread Starter mylesmarkevich

    (@mylesmarkevich)

    That sort of worked, thanks @ketanvyawahare! I have a question about its usage, though. It seems like I still have to keep my validation at the edit_user_profile_update action hook to prevent the invalid value from being saved? If I comment it out, I get the error message but the value gets saved anyways. So now I’m validating the input in two places… I just want to make sure that’s correct, or maybe there something else I’m supposed to be doing for efficiency?

    Also, after adding this new code, I get a PHP Notice about an undefined index for a different custom user meta field (a checkbox) whenever I try to send an invalid value… I don’t understand why that’s happening. Maybe someone might know?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How can I return an error message when updating user meta?’ is closed to new replies.