Add Custom Fields to WordPress User Profiles Easily

How to add custom fields to wordpress user profiles; Add custom fields to user profiles wordpress; Wordpress add custom user profile fields; Custom fields in wordpress user registration; Programmatically add custom fields to wordpress user profiles; Best plugin for custom user fields wordpress; Wordpress user profile custom fields tutorial; Add additional fields to wordpress user registration; Custom user profile fields wordpress plugin; How to customize wordpress user profiles;

Explanation

Want to add extra fields to user profiles in WordPress? This code snippet does just that by adding fields for a phone number and address.

Adding Fields to User Profiles:

  • When viewing or editing a user profile, two new fields appear: one for the phone number and another for the address.
  • These fields are added using hooks that display them on the user profile page.

Saving the Data:

  • When a user updates their profile, the data entered in these fields is saved securely.
  • The code checks if the current user has permission to edit the profile before saving.

Adding Fields to Registration:

  • New users can also enter their phone number and address during registration.
  • The fields are added to the registration form using a hook.

Saving Registration Data:

  • Once a new user registers, their phone number and address are saved to their profile.

This setup ensures that both existing and new users can have additional information stored in their profiles, making it easy to manage extra details without needing a plugin.

Code

<?php
// Hook to add custom fields to the user profile page
add_action('show_user_profile', 'wp_dudecom_add_custom_user_profile_fields');
add_action('edit_user_profile', 'wp_dudecom_add_custom_user_profile_fields');

function wp_dudecom_add_custom_user_profile_fields($user) {
    ?>
    <h3>Additional Information</h3>
    <table class="form-table">
        <tr>
            <th><label for="phone_number">Phone Number</label></th>
            <td>
                <input type="text" name="phone_number" id="phone_number" value="<?php echo esc_attr(get_the_author_meta('phone_number', $user->ID)); ?>" class="regular-text" /><br />
                <span class="description">Please enter your phone number.</span>
            </td>
        </tr>
        <tr>
            <th><label for="address">Address</label></th>
            <td>
                <input type="text" name="address" id="address" value="<?php echo esc_attr(get_the_author_meta('address', $user->ID)); ?>" class="regular-text" /><br />
                <span class="description">Please enter your address.</span>
            </td>
        </tr>
    </table>
    <?php
}

// Hook to save custom fields from the user profile page
add_action('personal_options_update', 'wp_dudecom_save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'wp_dudecom_save_custom_user_profile_fields');

function wp_dudecom_save_custom_user_profile_fields($user_id) {
    // Check if the current user has permission to edit the user profile
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }

    // Sanitize and update user meta data
    if (isset($_POST['phone_number'])) {
        update_user_meta($user_id, 'phone_number', sanitize_text_field($_POST['phone_number']));
    }

    if (isset($_POST['address'])) {
        update_user_meta($user_id, 'address', sanitize_text_field($_POST['address']));
    }
}

// Hook to add custom fields to the user registration form
add_action('register_form', 'wp_dudecom_add_custom_user_registration_fields');

function wp_dudecom_add_custom_user_registration_fields() {
    ?>
    <p>
        <label for="phone_number"><?php _e('Phone Number', 'wp-dudecom'); ?><br />
        <input type="text" name="phone_number" id="phone_number" class="input" value="<?php echo esc_attr(wp_unslash($_POST['phone_number'] ?? '')); ?>" size="25" /></label>
    </p>
    <p>
        <label for="address"><?php _e('Address', 'wp-dudecom'); ?><br />
        <input type="text" name="address" id="address" class="input" value="<?php echo esc_attr(wp_unslash($_POST['address'] ?? '')); ?>" size="25" /></label>
    </p>
    <?php
}

// Hook to save custom fields from the user registration form
add_action('user_register', 'wp_dudecom_save_custom_user_registration_fields');

function wp_dudecom_save_custom_user_registration_fields($user_id) {
    if (isset($_POST['phone_number'])) {
        update_user_meta($user_id, 'phone_number', sanitize_text_field($_POST['phone_number']));
    }

    if (isset($_POST['address'])) {
        update_user_meta($user_id, 'address', sanitize_text_field($_POST['address']));
    }
}
?>

Instructions

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites: Ensure you have access to your WordPress theme files or the ability to create a custom plugin.

Implementation Steps:

  1. Access Your WordPress Files:
    • Log in to your WordPress admin dashboard.
    • Navigate to Appearance > Theme Editor if you are adding to functions.php, or use an FTP client or file manager to access your WordPress files if creating a custom plugin.
  2. Add the Code:
    • Copy the provided code snippet.
    • Paste it into the functions.php file of your active theme or into a custom plugin file.
  3. Save Changes:
    • If using the Theme Editor, click Update File to save your changes.
    • If using a custom plugin, ensure the plugin is activated in the WordPress admin under Plugins > Installed Plugins.
  4. Verify Implementation:
    • Go to Users > Your Profile in the WordPress admin to see the new fields.
    • Test updating the fields and saving the profile to ensure data is stored correctly.
    • Test the registration form to ensure new users can enter their phone number and address.

If you need help with implementation or more advanced functionality, consider using the services of wp-dude.com.