Skip to content

Instantly share code, notes, and snippets.

@mattpramschufer
Created February 14, 2018 14:50
Show Gist options
  • Save mattpramschufer/8d4ea21d6a5199586eb31abac8b968b6 to your computer and use it in GitHub Desktop.
Save mattpramschufer/8d4ea21d6a5199586eb31abac8b968b6 to your computer and use it in GitHub Desktop.
Adding custom fields to Wordpress User Profile
<?php
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("LEGACY IMPORTED USER DATA", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e("Legacy Deposit Date"); ?></label></th>
<td>
<input type="text" name="_legacy_deposit_date" id="_legacy_deposit_date" value="<?php echo esc_attr( get_the_author_meta( '_legacy_deposit_date', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Deposit Date"); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("Legacy Phone"); ?></label></th>
<td>
<input type="text" name="_legacy_phone" id="_legacy_phone" value="<?php echo esc_attr( get_the_author_meta( '_legacy_phone', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Legacy Phone"); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e("Legacy Subscriber ID"); ?></label></th>
<td>
<input type="text" name="_legacy_subscriber_id" id="_legacy_subscriber_id" value="<?php echo esc_attr( get_the_author_meta( '_legacy_subscriber_id', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Subscriber ID."); ?></span>
</td>
</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 ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, '_legacy_deposit_date', $_POST['_legacy_deposit_date'] );
update_user_meta( $user_id, '_legacy_phone', $_POST['_legacy_phone'] );
update_user_meta( $user_id, '_legacy_subscriber_id', $_POST['_legacy_subscriber_id'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment