WordPress Backend: Add Custom Field to Users Page

So this is to add a custom field to your wordpress users page on the admin side. For well a plethora of things come to mind, if you want to show custom usermeta and make them editatble on the back end i’m guessing this would be pretty useful no?

Ok the first part the function digthis_add_custom_fields just basically outputs the form and everything else required to show your field on the back end.

<?php function digthis_add_custom_fields( $user ) {?>

<h3><?php _e('Maintenance', 'twentythirteen'); ?></h3>

<table class="form-table">
<tr>
<th>
<label for=""><?php _e('Digthis Custom Field', 'twentythirteen'); ?>
</label></th>
<td>
<input id=''my_id'  type='text' name="fbname" value="facebook" /><br />
<span class="description"><?php _e('Enter your faceboo id here', 'twentythirteen'); ?></span>
</td>
</tr>
</table>
<?php }

Now the second part

add_action( 'show_user_profile', 'digthis_add_custom_fields' );
add_action( 'edit_user_profile', 'digthis_add_custom_fields' );
  • The above code specifies two things ‘show_user_profile’ specifies that when you view your own profile your field will be visible
  • The second determines whether or not the field is visible when an administrator views a user
add_action( 'edit_user_profile_update', 'digthis_save_this_field' );
add_action( 'personal_options_update', 'digthis_save_this_field' );

function dithis_save_this_field($user_id){
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;

$updatefb = $_POST['fbname];

update_uset_meta('$user_id','facebook',$updatefb);
}

This last part is basically a save field, on updating a user the function digthis_save_this_field() will be called
Sanitize you values here (I haven’t bothered) and your data will be stored wherever you please for simplicity i’ve just created a new user_meta called facebook and updated it there.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.