最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I return an error message when updating user meta?

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

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;
}
发布评论

评论列表(0)

  1. 暂无评论