Customize User Profile Fields

Hire Now!

Whether you’re looking to launch your brand, showcase your portfolio, or open an online store, we’re here to bring your ideas to life.

  • Post Created: 1 week ago
  • Views: 2

Customize User Profile Fields

Reading Time: 2 Minutes

Understanding the Hook: show_user_profile and edit_user_profile

These hooks allow you to add custom fields to the user profile page in the admin area.

Why Use This Hook?

  • Extend user profiles with additional data fields.
  • Add features like social media links or preferences.

How It Works

Add fields to the profile page and save them using the personal_options_update and edit_user_profile_update actions.

Code Example

function add_custom_user_profile_fields($user) {
    ?>
    <h3>Extra Information</h3>
    <table class="form-table">
        <tr>
            <th><label for="twitter">Twitter Handle</label></th>
            <td>
                <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr(get_user_meta($user->ID, 'twitter', true)); ?>" />
            </td>
        </tr>
    </table>
    <?php
}
add_action('show_user_profile', 'add_custom_user_profile_fields');
add_action('edit_user_profile', 'add_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, 'twitter', sanitize_text_field($_POST['twitter']));
}
add_action('personal_options_update', 'save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'save_custom_user_profile_fields');

Discussion