I am saving extra profile fields to my users' profiles - not via User admin but via another method (a plugin, but that's not important - all the user data is there properly).
Now I do also want to show the extra info on User profiles in the admin backend, and make them editable there, too. I have followed the Justin Tadlock code at http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields - that means I have added some actions and two functions to functions.php, to support displaying and updating these fields on the backend.
The problem is, fields whose names include underscores are not returning results properly.
ie. Whilst I can properly return the value of "company", the value of "company_url" is also returned as though it were "company" (eg. "Microsoft" instead of "http://www.microsoft.com").
My two functions are below. I haven't yet updated the save function as this would just corrupt the User data with a duplicate of the "company" value. I have many field names containing "_", so this will be important.
/*
* Extra profile fields: Show on user admin backend
* http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
*/
add_action( 'show_user_profile', 'mysite_show_user_meta' );
add_action( 'edit_user_profile', 'mysite_show_extra_profile_fields' );
function mysite_show_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Twitter</label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your Twitter username.</span>
</td>
</tr>
<tr>
<th><label for="company">Company</label></th>
<td>
<input type="text" name="company" id="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your company.</span>
</td>
</tr>
<tr>
<th><label for="company_url">Company website</label></th>
<td>
<input type="text" name="company_url" id="company_url" value="<?php echo esc_attr( get_the_author_meta( 'company_url', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your company website URL.</span>
</td>
</tr>
<tr>
<th><label for="photo_url">Photo URL</label></th>
<td>
<input type="text" name="photo_url" id="photo_url" value="<?php echo esc_attr( get_the_author_meta( 'photo_url', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your photo URL. </span>
</td>
</tr>
</table>
<?php }
/*
* Extra profile fields: Save
* http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
*/
add_action( 'personal_options_update', 'mysite_user_meta' );
add_action( 'edit_user_profile_update', 'mysite_user_meta' );
function mysite_user_meta( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
}