admin管理员组文章数量:1435859
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);
}
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);
}
Share
Improve this question
asked Apr 19, 2020 at 12:40
MylesMyles
3311 silver badge7 bronze badges
1 Answer
Reset to default 0// User Settings Errors
add_filter( 'user_profile_update_errors', 'my_plugin_user_settings_errors' );
function my_plugin_user_settings_errors( $errors ) {
$user_level = intval( $_POST[ 'my-plugin-user-level' ] );
if ( ! is_int( $user_level ) || $user_level < 0 || $user_level > 50 ) {
$errors->add( 'my-plugin-invalid-admin-level', '<strong>ERROR:</strong> User Level must be between 0 and 50.' );
}
return $errors;
}
本文标签: How can I return an error message when updating user meta
版权声明:本文标题:How can I return an error message when updating user meta? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744544526a2611818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论