DEV Community

Robiul Awal
Robiul Awal

Posted on

Understanding and Implementing Custom User Profile Fields in WordPress

The provided code defines a class named WSEL_User_Fields in WordPress that facilitates the addition of custom fields to user profiles. These fields include "Membership number," "Join Date," and "Re Registration Date." Let's break down the code and understand how it works:

Class Initialization:

if ( ! class_exists( 'WSEL_User_Fields' ) ) :
    class WSEL_User_Fields {
Enter fullscreen mode Exit fullscreen mode

This conditional check ensures that the class WSEL_User_Fields is defined only if it doesn't already exist.

Constructor:

public function __construct() {
    add_action( 'show_user_profile', array( $this, 'wsel_profile_fields' ), 999 );
    add_action( 'edit_user_profile', array( $this, 'wsel_profile_fields' ), 999 );

    add_action( 'personal_options_update', array( $this, 'wsel_save_profile_fields' ) );
    add_action( 'edit_user_profile_update', array( $this, 'wsel_save_profile_fields' ) );
}
Enter fullscreen mode Exit fullscreen mode

The constructor sets up various WordPress actions to display and save the custom user profile fields. The show_user_profile and edit_user_profile actions are used to display the fields, while personal_options_update and edit_user_profile_update are used to save the field values.

Saving Profile Fields:

public function wsel_save_profile_fields( $user_id ) {
    // Checks for security nonce and user capability
    if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
        return;
    }

    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return;
    }

    // Update user meta with sanitized field values
    update_user_meta( $user_id, 'wsel_membership_number', sanitize_text_field( $_POST['wsel_membership_number'] ) );
    update_user_meta( $user_id, 'wsel_join_date', sanitize_text_field( $_POST['wsel_join_date'] ) );
    update_user_meta( $user_id, 'wsel_re_registration_date', sanitize_text_field( $_POST['wsel_re_registration_date'] ) );
}
Enter fullscreen mode Exit fullscreen mode

This function handles the saving of custom field values when a user updates their profile. It checks for security nonce verification and user capabilities before updating the user meta with the sanitized values.

Displaying Profile Fields:

public function wsel_profile_fields( $user ) {
    // Retrieve custom field values
    $wsel_membership_number    = get_user_meta( $user->ID, 'wsel_membership_number', true );
    $wsel_join_date            = get_user_meta( $user->ID, 'wsel_join_date', true );
    $wsel_re_registration_date = get_user_meta( $user->ID, 'wsel_re_registration_date', true );
    ?>
    <!-- HTML markup to display the custom fields -->
    <h3>Additional Information</h3>
    <table class="form-table">
        <!-- Membership number field -->
        <tr>
            <th><label for="wsel_membership_number">Membership number</label></th>
            <td>
                <input type="text" name="wsel_membership_number" id="wsel_membership_number" value="<?php echo esc_attr( $wsel_membership_number ); ?>" class="regular-text" />
            </td>
        </tr>
        <!-- Join Date field -->
        <tr>
            <th><label for="wsel_join_date">Join Date</label></th>
            <td>
                <input type="text" name="wsel_join_date" id="wsel_join_date" value="<?php echo esc_attr( $wsel_join_date ); ?>" class="regular-text" />
            </td>
        </tr>
        <!-- Re Registration Date field -->
        <tr>
            <th><label for="wsel_re_registration_date">Re Registration Date</label></th>
            <td>
                <input type="text" name="wsel_re_registration_date" id="wsel_re_registration_date" value="<?php echo esc_attr( $wsel_re_registration_date ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
    <?php
}
Enter fullscreen mode Exit fullscreen mode

This function generates the HTML markup for displaying the custom fields on the user profile page. It retrieves the stored values and populates the input fields accordingly.

Conclusion:

In summary, the provided code defines a WordPress class that adds custom fields to user profiles, allowing users to input and save additional information. The fields are displayed on the user profile page, and the entered data is stored securely in the user meta. This functionality can be useful for websites that require specific user details beyond the default WordPress profile fields.

Top comments (0)