What I’m trying to do:
- Add new user meta fields that can be updated when the user edits their profile
- Validate them correctly when the form is submitted
What I’ve done:
I have successfully added the fields to the user editting form and have them being updated to the options database as follows:
public function setup_user_meta() {
// Show the fields when editing or showing
add_action('show_user_profile', array($this,'show_user_meta'));
add_action('edit_user_profile', array($this,'show_user_meta'));
// Update the fields
add_action('personal_options_update', array($this,'process_option_update'));
add_action('edit_user_profile_update', array($this,'process_option_update'));
}
.
public function show_user_meta($user) {
?>
<h3>Player Information</h3>
<table class="form-table">
<tbody>
<tr>
<th><label for="cuhc-description">Description</label></th>
<td><textarea name="cuhc-description" id="cuhc-description" rows="5" cols="30"><?php echo esc_textarea(get_the_author_meta('cuhc-description', $user->ID)); ?></textarea>
</tr>
</tbody>
</table>
<?php
}
.
public function process_option_update($userid) {
update_user_meta($userid, 'cuhc-description', ( isset($_POST['cuhc-description']) ? $_POST['cuhc-description'] : '' ) );
}
What I want:
This works, however I have a number of different fields and I want to validate them in process_option_update()
, eg: checking they’re a number and if not returning an error to the user.
How do I tell WordPress there is an error and not to continue to act on the form? And where do I echo or send the error(s)? WordPress also appears to use javascript validation within the user update page – how do I piggy-back onto this instead of doing my own one?