How can I return an error message when updating user meta?
-
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); }
- The topic ‘How can I return an error message when updating user meta?’ is closed to new replies.