Using WordPress ‘personal_options_update’ PHP action

The personal_options_update WordPress PHP action fires before the page loads on the ‘Profile’ editing screen. This action only triggers if the current user is editing their own profile.

Usage

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

Parameters

  • $user_id (int) – The user ID.

More information

See WordPress Developer Resources: personal_options_update

Examples

Update user’s custom field

Update a custom field for the user when they update their profile.

add_action('personal_options_update', 'update_custom_field');
function update_custom_field($user_id) {
  // Get the custom field value from $_POST
  $custom_field_value = $_POST['custom_field'];

  // Update the custom field
  update_user_meta($user_id, 'custom_field_key', $custom_field_value);
}

Send email notification on profile update

Send an email notification to the administrator when a user updates their profile.

add_action('personal_options_update', 'send_profile_update_email');
function send_profile_update_email($user_id) {
  // Prepare email content
  $subject = 'User Profile Updated';
  $message = 'User ID ' . $user_id . ' has updated their profile.';
  $admin_email = get_option('admin_email');

  // Send the email
  wp_mail($admin_email, $subject, $message);
}

Log profile update to a custom log file

Log profile updates to a custom log file.

add_action('personal_options_update', 'log_profile_update');
function log_profile_update($user_id) {
  // Prepare log entry
  $log_entry = date('Y-m-d H:i:s') . ' - User ID ' . $user_id . ' updated their profile.' . PHP_EOL;

  // Append log entry to the log file
  file_put_contents('/path/to/logfile.log', $log_entry, FILE_APPEND);
}

Set user role based on a custom field

Set a user’s role based on the value of a custom field in their profile.

add_action('personal_options_update', 'set_user_role_based_on_custom_field');
function set_user_role_based_on_custom_field($user_id) {
  // Get custom field value
  $custom_field_value = get_user_meta($user_id, 'custom_field_key', true);

  // Get the user object
  $user = new WP_User($user_id);

  // Set user role based on custom field value
  if ($custom_field_value === 'value1') {
    $user->set_role('subscriber');
  } elseif ($custom_field_value === 'value2') {
    $user->set_role('contributor');
  }
}

Redirect user after profile update

Redirect the user to a custom URL after updating their profile.

add_action('personal_options_update', 'redirect_after_profile_update');
function redirect_after_profile_update($user_id) {
  // Set the custom redirect URL
  $redirect_url = home_url('/custom-page/');

  // Redirect the user
  wp_redirect($redirect_url);
  exit;
}

Save custom date picker data from the Personal Options

Save the value of the “Date of Birth” date picker when the user updates their profile.

add_action('personal_options_update', 'save_date_of_birth_picker');
function save_date_of_birth_picker($user_id) {
  // Check if the current user has the capability to edit this user
  if (!current_user_can('edit_user', $user_id)) {
    return false;
  }

  // Update the user's date of birth
  update_user_meta($user_id, 'date_of_birth', $_POST['date_of_birth']);
}

Save custom dropdown data from the Personal Options

Save the value of the “Preferred Language” dropdown when the user updates their profile.

add_action('personal_options_update', 'save_preferred_language_dropdown');
function save_preferred_language_dropdown($user_id) {
  // Check if the current user has the capability to edit this user
  if (!current_user_can('edit_user', $user_id)) {
    return false;
  }

  // Update the user's preferred language
  update_user_meta($user_id, 'preferred_language', $_POST['preferred_language']);
}

Save custom checkbox data from the Personal Options

Save the value of the “Enable Dark Mode” checkbox when the user updates their profile.

add_action('personal_options_update', 'save_dark_mode_checkbox');
function save_dark_mode_checkbox($user_id) {
  // Check if the current user has the capability to edit this user
  if (!current_user_can('edit_user', $user_id)) {
    return false;
  }

  // Update the user's dark mode preference
  $enable_dark_mode = isset($_POST['enable_dark_mode']) ? 1 : 0;
  update_user_meta($user_id, 'enable_dark_mode', $enable_dark_mode);
}

Save custom field data from the Personal Options

Save the value of the “Favorite Color” field when the user updates their profile.

add_action('personal_options_update', 'save_favorite_color_field');
function save_favorite_color_field($user_id) {
  // Check if the current user has the capability to edit this user
  if (!current_user_can('edit_user', $user_id)) {
    return false;
  }

  // Update the user's favorite color
  update_user_meta($user_id, 'favorite_color', $_POST['favorite_color']);
}

Save custom field data

This example saves the custom field data when the profile is updated.

add_action('personal_options_update', 'save_favorite_color_field');
function save_favorite_color_field($user_id) {
    if (current_user_can('edit_user', $user_id)) {
        update_user_meta($user_id, 'favorite_color', sanitize_text_field($_POST['favorite_color']));
    }
}