User meta information, like that on the profile page, is stored in the usermeta
table in the database. You can add extra fields by hooking into the show_user_profile
and edit_user_profile
action hooks. Then you can save the information by hooking into the personal_options_update
and edit_user_profile_update
action hooks with a function that updates the new field in the user meta table, update_user_meta()
.
First create the fields to be rendered on the profile page
//display extra profile fields
add_action( 'show_user_profile', 'extra_profile_fields' );
add_action( 'edit_user_profile', 'extra_profile_fields' );
function extra_profile_fields( $user )
{
?>
<h3>Extra Profile Fields</h3>
<table class="form-table">
<tr>
<th><label for="UID-Nummer"><?php _e( 'UID-Nummer', 'woocommerce' ); ?></label></th>
<td><input type="text" name="uid-nummer" value="<?php echo esc_attr( get_the_author_meta( 'uid-nummer', $user->ID ) ); ?>" class="regular-text" /></td>
</tr>
</table>
<?php
}
Note the name
attribute. It’s a good idea to use this same name when saving the field. You can see that we are pulling that field into the value already using get_the_author_meta( 'uid-nummer', $user->ID )
.
Save the fields
//save extra profile fields
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id )
{
update_user_meta( $user_id,'uid-nummer', sanitize_text_field( $_POST['uid-nummer'] ) );
}
Using the fields
To display the field ui-nummer
you can use either:
//return the author meta
get_the_author_meta( 'ui-nummer', $user->ID );
or
//echo the author meta
the_author_meta( 'ui-nummer', $user->ID );