Using WordPress ‘edit_user_profile’ PHP action

The edit_user_profile WordPress PHP action hook allows you to add new fields or data to the bottom of a user profile page in the WordPress admin area.

Usage

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

Parameters

  • $profile_user (WP_User) – The current WP_User object.

More information

See WordPress Developer Resources: edit_user_profile

This action hook only triggers when a user is viewing another user’s profile page (not their own). If you want to apply your hook to ALL profile pages (including the current user), then you also need to use the show_user_profile hook.

Examples

Add a Custom Field to the User Profile

This code adds a custom field called “Favorite Color” to the user profile page.

add_action('edit_user_profile', 'add_favorite_color_field');
function add_favorite_color_field($profile_user) {
  $favorite_color = get_user_meta($profile_user->ID, 'favorite_color', true);
  echo '<h3>Extra Profile Information</h3>';
  echo '<table class="form-table">';
  echo '<tr>';
  echo '<th><label for="favorite_color">Favorite Color</label></th>';
  echo '<td><input type="text" name="favorite_color" id="favorite_color" value="' . esc_attr($favorite_color) . '" class="regular-text" /><br /></td>';
  echo '</tr>';
  echo '</table>';
}

Display the Number of Published Posts

This code displays the number of published posts for the user.

add_action('edit_user_profile', 'display_published_posts_count');
function display_published_posts_count($profile_user) {
  $post_count = count_user_posts($profile_user->ID);
  echo '<h3>Post Statistics</h3>';
  echo '<p>Published Posts: ' . $post_count . '</p>';
}

Show User’s Registration Date

This code shows the registration date of the user.

add_action('edit_user_profile', 'display_user_registration_date');
function display_user_registration_date($profile_user) {
  $registration_date = date('F j, Y', strtotime($profile_user->user_registered));
  echo '<h3>User Registration Date</h3>';
  echo '<p>Registered on: ' . $registration_date . '</p>';
}

Display Custom Taxonomy Terms

This code displays the terms of a custom taxonomy associated with the user.

add_action('edit_user_profile', 'display_custom_taxonomy_terms');
function display_custom_taxonomy_terms($profile_user) {
  $terms = wp_get_object_terms($profile_user->ID, 'your_custom_taxonomy');
  echo '<h3>Custom Taxonomy Terms</h3>';
  echo '<ul>';
  foreach ($terms as $term) {
    echo '<li>' . $term->name . '</li>';
  }
  echo '</ul>';
}

Show User’s Last Login Time

This code shows the last login time of the user.

add_action('edit_user_profile', 'display_last_login_time');
function display_last_login_time($profile_user) {
  $last_login = get_user_meta($profile_user->ID, 'last_login', true);
  if (!empty($last_login)) {
    $last_login_time = date('F j, Y, g:i a', strtotime($last_login));
    echo '<h3>Last Login Time</h3>';
    echo '<p>Last login: ' . $last
_login_time . '</p>';
} else {
echo '<h3>Last Login Time</h3>';
echo '<p>No login data available.</p>';
}
}

Note: To make this example work, you’ll need to save the user’s last login time in the user meta when they log in. You can use the following code snippet to achieve this:

add_action('wp_login', 'save_last_login_time', 10, 2);
function save_last_login_time($user_login, $user) {
  update_user_meta($user->ID, 'last_login', current_time('mysql'));
}