0

I created Custom User Fields on a User Profile Using this tutorial.

How do I add a field on the Users profile? For example, country, age etc.

I created three custom fields with the names: "ID","Message", "Kilometers" on my WordPress site on the User -> Profile Tab,

like so:

enter image description here

The code in my Theme File Editor in my Wordpress page for those three fields(Theme Functions - functions.php) looks like this:

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="address"><?php _e("ID"); ?></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"><?php _e("Please enter your ID."); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("Message"); ?></label></th>
<td>
<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your message."); ?></span>
</td>
</tr>
<tr>
<th><label for="province"><?php _e("Kilometers"); ?></label></th>
<td>
<input type="text" name="province" id="province" value="<?php echo esc_attr( get_the_author_meta( 'province', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your kilometers."); ?></span>
</td>
</tr>
<tr>
</tr>
</table>
<?php }

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {
    if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
        return;
    }
    
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
        return false; 
    }
    update_user_meta( $user_id, 'address', $_POST['address'] );
    update_user_meta( $user_id, 'city', $_POST['city'] );
    update_user_meta( $user_id, 'postalcode', $_POST['postalcode'] );
} 

I would like to display the output from those fields on a user custom user page.

So for example I am saving the output from those three custom fields and I enter my Wordpress site I choose a custom user page and I would like the output from those three fields to appear there. Ideally I would like to display the output in a table but if that's not possible I am open to any solution really.

Many Thanks!

1
  • Are you developing a first-party theme/child theme? If not, note that your modifications to the theme's files will be overwritten as soon as the theme updates.
    – bosco
    Aug 10, 2022 at 15:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.