You can extend user profiles in DigiFusion by adding custom fields to the user edit screen. This allows you to collect additional information about users beyond the default WordPress fields.
Adding Custom Profile Fields
Add this code to your child theme’s functions.php
file:
/**
* Add custom fields to user profile
*/
function digifusion_child_add_custom_user_fields( $user ) {
?>
<h3><?php esc_html_e( 'Additional Information', 'digifusion-child' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="company"><?php esc_html_e( 'Company', 'digifusion-child' ); ?></label></th>
<td>
<input type="text" name="company" id="company" value="<?php echo esc_attr( get_user_meta( $user->ID, 'company', true ) ); ?>" class="regular-text" />
<p class="description"><?php esc_html_e( 'Enter your company name.', 'digifusion-child' ); ?></p>
</td>
</tr>
<tr>
<th><label for="job_title"><?php esc_html_e( 'Job Title', 'digifusion-child' ); ?></label></th>
<td>
<input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_user_meta( $user->ID, 'job_title', true ) ); ?>" class="regular-text" />
<p class="description"><?php esc_html_e( 'Enter your job title.', 'digifusion-child' ); ?></p>
</td>
</tr>
<tr>
<th><label for="phone_number"><?php esc_html_e( 'Phone Number', 'digifusion-child' ); ?></label></th>
<td>
<input type="tel" name="phone_number" id="phone_number" value="<?php echo esc_attr( get_user_meta( $user->ID, 'phone_number', true ) ); ?>" class="regular-text" />
<p class="description"><?php esc_html_e( 'Enter your phone number.', 'digifusion-child' ); ?></p>
</td>
</tr>
<tr>
<th><label for="address"><?php esc_html_e( 'Address', 'digifusion-child' ); ?></label></th>
<td>
<textarea name="address" id="address" rows="3" cols="30"><?php echo esc_textarea( get_user_meta( $user->ID, 'address', true ) ); ?></textarea>
<p class="description"><?php esc_html_e( 'Enter your address.', 'digifusion-child' ); ?></p>
</td>
</tr>
<tr>
<th><label for="date_of_birth"><?php esc_html_e( 'Date of Birth', 'digifusion-child' ); ?></label></th>
<td>
<input type="date" name="date_of_birth" id="date_of_birth" value="<?php echo esc_attr( get_user_meta( $user->ID, 'date_of_birth', true ) ); ?>" />
<p class="description"><?php esc_html_e( 'Enter your date of birth.', 'digifusion-child' ); ?></p>
</td>
</tr>
<tr>
<th><label for="user_skills"><?php esc_html_e( 'Skills', 'digifusion-child' ); ?></label></th>
<td>
<input type="text" name="user_skills" id="user_skills" value="<?php echo esc_attr( get_user_meta( $user->ID, 'user_skills', true ) ); ?>" class="regular-text" />
<p class="description"><?php esc_html_e( 'Enter your skills (comma-separated).', 'digifusion-child' ); ?></p>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'digifusion_child_add_custom_user_fields' );
add_action( 'edit_user_profile', 'digifusion_child_add_custom_user_fields' );
Saving Custom Profile Fields
Add this code to save the custom field data:
/**
* Save custom user profile fields
*/
function digifusion_child_save_custom_user_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
$custom_fields = array(
'company',
'job_title',
'phone_number',
'address',
'date_of_birth',
'user_skills'
);
foreach ( $custom_fields as $field ) {
if ( isset( $_POST[ $field ] ) ) {
if ( 'address' === $field ) {
update_user_meta( $user_id, $field, sanitize_textarea_field( wp_unslash( $_POST[ $field ] ) ) );
} else {
update_user_meta( $user_id, $field, sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) );
}
}
}
return true;
}
add_action( 'personal_options_update', 'digifusion_child_save_custom_user_fields' );
add_action( 'edit_user_profile_update', 'digifusion_child_save_custom_user_fields' );
Retrieving Custom Field Data
To display custom user fields in your templates, use this code:
// Get current user data
$user_id = get_current_user_id();
// Retrieve custom fields
$company = get_user_meta( $user_id, 'company', true );
$job_title = get_user_meta( $user_id, 'job_title', true );
$phone_number = get_user_meta( $user_id, 'phone_number', true );
$address = get_user_meta( $user_id, 'address', true );
$date_of_birth = get_user_meta( $user_id, 'date_of_birth', true );
$user_skills = get_user_meta( $user_id, 'user_skills', true );
// Display the data
if ( $company ) {
echo '<p><strong>Company:</strong> ' . esc_html( $company ) . '</p>';
}
if ( $job_title ) {
echo '<p><strong>Job Title:</strong> ' . esc_html( $job_title ) . '</p>';
}
if ( $phone_number ) {
echo '<p><strong>Phone:</strong> ' . esc_html( $phone_number ) . '</p>';
}
if ( $address ) {
echo '<p><strong>Address:</strong> ' . esc_html( wpautop( $address ) ) . '</p>';
}
if ( $date_of_birth ) {
echo '<p><strong>Date of Birth:</strong> ' . esc_html( date( 'F j, Y', strtotime( $date_of_birth ) ) ) . '</p>';
}
if ( $user_skills ) {
$skills_array = explode( ',', $user_skills );
echo '<p><strong>Skills:</strong> ';
foreach ( $skills_array as $skill ) {
echo '<span class="skill-tag">' . esc_html( trim( $skill ) ) . '</span> ';
}
echo '</p>';
}
Adding Validation
To add validation for specific fields, modify the save function:
function digifusion_child_save_custom_user_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
// Validate phone number format
if ( isset( $_POST['phone_number'] ) ) {
$phone = sanitize_text_field( wp_unslash( $_POST['phone_number'] ) );
if ( ! empty( $phone ) && ! preg_match( '/^[\d\s\-\+\(\)]+$/', $phone ) ) {
add_action( 'user_profile_update_errors', function( $errors ) {
$errors->add( 'phone_number_error', __( 'Please enter a valid phone number.', 'digifusion-child' ) );
});
return;
}
update_user_meta( $user_id, 'phone_number', $phone );
}
// Validate date of birth
if ( isset( $_POST['date_of_birth'] ) ) {
$dob = sanitize_text_field( wp_unslash( $_POST['date_of_birth'] ) );
if ( ! empty( $dob ) && ! DateTime::createFromFormat( 'Y-m-d', $dob ) ) {
add_action( 'user_profile_update_errors', function( $errors ) {
$errors->add( 'dob_error', __( 'Please enter a valid date.', 'digifusion-child' ) );
});
return;
}
update_user_meta( $user_id, 'date_of_birth', $dob );
}
// Save other fields without special validation
$simple_fields = array( 'company', 'job_title', 'user_skills' );
foreach ( $simple_fields as $field ) {
if ( isset( $_POST[ $field ] ) ) {
update_user_meta( $user_id, $field, sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) );
}
}
// Save address with textarea sanitization
if ( isset( $_POST['address'] ) ) {
update_user_meta( $user_id, 'address', sanitize_textarea_field( wp_unslash( $_POST['address'] ) ) );
}
return true;
}
The custom fields will now appear in the user profile edit screen for both the profile owner and administrators. All data is properly sanitized and validated before being saved to the database.