Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkkr/064cc43505ebbfdc6f66b03e34f922b9 to your computer and use it in GitHub Desktop.
Save mrkkr/064cc43505ebbfdc6f66b03e34f922b9 to your computer and use it in GitHub Desktop.
Add column with custom user meta in admin panel and edit this in edit profile page
<?php
/* Add custom column */
function woo_add_user_nip_column( $columns ) { // e.g. NIP
$columns['billing_company_nip'] = __( 'NIP', 'theme' );
return $columns;
}
add_filter( 'manage_users_columns', 'woo_add_user_nip_column' );
function woo_show_user_nip_data( $value, $column_name, $user_id ) {
if( 'billing_company_nip' == $column_name ) {
return get_user_meta( $user_id, 'billing_company_nip', true );
}
}
add_action( 'manage_users_custom_column', 'woo_show_user_nip_data', 10, 3 );
/* add custom user meta in edit user profile */
add_action( 'show_user_profile', 'my_custom_user_profile_field' );
add_action( 'edit_user_profile', 'my_custom_user_profile_field' );
function my_custom_user_profile_field( $user ) { ?>
<table class="form-table">
<tr>
<th><label for="company-nip">NIP:</label></th>
<td>
<input name="billing_company_nip" id="company-nip" value="<?php echo esc_attr( get_the_author_meta( 'billing_company_nip', $user->ID ) ); ?>" class="regular-text" type="text">
</td>
</tr>
</table>
<?php }
/* update custom user meta */
add_action( 'personal_options_update', 'save_my_custom_user_profile_field' );
add_action( 'edit_user_profile_update', 'save_my_custom_user_profile_field' );
function save_my_custom_user_profile_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( absint( $user_id ), 'billing_company_nip', wp_kses_post( $_POST['billing_company_nip'] ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment