Using WordPress ‘edit_user_profile_update’ PHP action

The edit_user_profile_update WordPress PHP action fires before the page loads on the ‘Edit User’ screen.

Usage

add_action('edit_user_profile_update', 'my_custom_function');
function my_custom_function($user_id) {
  // your custom code here
}

Parameters

  • $user_id (int) – The user ID.

More information

See WordPress Developer Resources: edit_user_profile_update

Examples

Save a custom field to the user profile

This example saves a custom field called ‘phone_number’ to the user profile when the ‘Edit User’ screen is updated.

// Add custom field to the 'Edit User' screen
add_action('edit_user_profile', 'add_phone_number_field');
function add_phone_number_field($user) {
  $phone_number = get_the_author_meta('phone_number', $user->ID);
  echo '<h3>Extra profile information</h3>';
  echo '<table class="form-table">';
  echo '<tr>';
  echo '<th><label for="phone_number">Phone number</label></th>';
  echo '<td>';
  echo '<input type="text" name="phone_number_data" id="phone_number" value="' . esc_attr($phone_number) . '" class="regular-text" />';
  echo '</td>';
  echo '</tr>';
  echo '</table>';
}

// Save the custom field when the profile is updated
add_action('edit_user_profile_update', 'save_phone_number_field');
function save_phone_number_field($user_id) {
  if (current_user_can('edit_user', $user_id)) {
    update_user_meta($user_id, 'phone_number', $_POST['phone_number_data']);
  }
}

Validate custom field input before saving

This example adds a custom field ‘zipcode’ and validates the input before saving it to the user profile.

// Add custom field to the 'Edit User' screen
add_action('edit_user_profile', 'add_zipcode_field');
function add_zipcode_field($user) {
  $zipcode = get_the_author_meta('zipcode', $user->ID);
  echo '<h3>Extra profile information</h3>';
  echo '<table class="form-table">';
  echo '<tr>';
  echo '<th><label for="zipcode">Zipcode</label></th>';
  echo '<td>';
  echo '<input type="text" name="zipcode_data" id="zipcode" value="' . esc_attr($zipcode) . '" class="regular-text" />';
  echo '</td>';
  echo '</tr>';
  echo '</table>';
}

// Validate and save the custom field when the profile is updated
add_action('edit_user_profile_update', 'save_zipcode_field');
function save_zipcode_field($user_id) {
  if (current_user_can('edit_user', $user_id)) {
    if (is_numeric($_POST['zipcode_data'])) {
      update_user_meta($user_id, 'zipcode', $_POST['zipcode_data']);
    } else {
      wp_die('Error: Zipcode must be a number.');
    }
  }
}