The "edit_user_profile_update" hook in WordPress is used to update user profile information when a user updates their profile from the WordPress dashboard. This hook is triggered after a user submits their updated profile form and before the updated information is saved to the database.
Developers can use this hook to add custom validation or processing of user data before it is saved to the database. This is especially useful when developers need to add custom fields to user profiles and want to ensure that the data entered in these fields is validated before being saved.
An example usage of the "edit_user_profile_update" hook would be to add custom fields to the user profile page and then validate the data entered in these fields before it is saved. Here’s an example code snippet that demonstrates how to use this hook to add a custom field for the user’s age to the user profile page:
function custom_user_profile_fields( $user ) {
?>
<h3><?php _e('Custom User Profile Fields'); ?></h3>
<table class="form-table">
<tr>
<th><label for="age"><?php _e('Age'); ?></label></th>
<td>
<input type="text" name="age" id="age" value="<?php echo esc_attr( get_the_author_meta( 'age', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Please enter your age.'); ?></span>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
function save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, 'age', $_POST['age'] );
}
add_action( 'personal_options_update', 'save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_user_profile_fields' );
In this example code, we first define a function "custom_user_profile_fields" which adds the custom field for the user’s age to the user profile page. We then use the "show_user_profile" and "edit_user_profile" actions to hook this function into the user profile page.
Next, we define a function "save_custom_user_profile_fields" which is responsible for saving the data entered in the custom field to the database. We use the "personal_options_update" and "edit_user_profile_update" actions to hook this function into the user profile page.
With this code in place, whenever a user updates their profile and submits the updated form, WordPress will trigger the "edit_user_profile_update" hook and run the "save_custom_user_profile_fields" function to validate and save the custom user profile field data to the database.