Add Custom User-Meta to the Users Admin page in WordPress

I have created a special form within my site that allows my users to enter a key. I am using add_user_meta() to add the meta data into the database. I want to be able to see this key when I click on users in the admin center.

How would I go about adding to this column?

Read More

Below is the meta data info im using

add_user_meta($userId,'code','12345');

We just want to be able to add it to the view on users.php in the table displaying username email and role.

I have used code like this to display the user id but I can not figure out how to display their meta.

add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
    $columns['user_id'] = 'User ID';
    return $columns;
}

add_action('manage_users_custom_column',  'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ( 'user_id' == $column_name )
        return $user_id;
    return $value;
}

Related posts

5 comments

  1. This example was created with the help of these two pages from the WordPress codex.

    https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile
    https://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update

    It is for displaying and updating the custom user meta data.

    <?php
    
    // Hooks near the bottom of profile page (if current user) 
    add_action('show_user_profile', 'custom_user_profile_fields');
    
    // Hooks near the bottom of the profile page (if not current user) 
    add_action('edit_user_profile', 'custom_user_profile_fields');
    
    // @param WP_User $user
    function custom_user_profile_fields( $user ) {
    ?>
        <table class="form-table">
            <tr>
                <th>
                    <label for="code"><?php _e( 'Custom Meta' ); ?></label>
                </th>
                <td>
                    <input type="text" name="code" id="code" value="<?php echo esc_attr( get_the_author_meta( 'code', $user->ID ) ); ?>" class="regular-text" />
                </td>
            </tr>
        </table>
    <?php
    }
    
    
    // Hook is used to save custom fields that have been added to the WordPress profile page (if current user) 
    add_action( 'personal_options_update', 'update_extra_profile_fields' );
    
    // Hook is used to save custom fields that have been added to the WordPress profile page (if not current user) 
    add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );
    
    function update_extra_profile_fields( $user_id ) {
        if ( current_user_can( 'edit_user', $user_id ) )
            update_user_meta( $user_id, 'code', $_POST['code'] );
    }
    
    ?>
    
  2. The answer above from Mordred worked for me after changing the second add_filter to add_action. Here’s the modified code:

    function yoursite_manage_users_columns( $columns ) {
    
        // $columns is a key/value array of column slugs and names
        $columns[ 'custom_field' ] = 'Subscription';
    
        return $columns;
    }
    
    add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
    
    function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
    
        switch ( $column_key ) {
            case 'custom_field':
                $value = get_user_meta( $user_id, 'custom_field', true );
    
                return $value;
                break;
            default: break;
        }
    
        // if no column slug found, return default output value
        return $output;
    }
    
    add_action( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
    
  3. To add custom user_meta fields to users.php you need to do the following:

    function yoursite_manage_users_columns( $columns ) {
    
        // $columns is a key/value array of column slugs and names
        $columns[ 'custom_field' ] = 'Subscription';
    
        return $columns;
    }
    
    add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
    
    function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
    
        switch ( $column_key ) {
            case 'custom_field':
                $value = get_user_meta( $user_id, 'custom_field', true );
    
                return $value;
                break;
            default: break;
        }
    
        // if no column slug found, return default output value
        return $output;
    }
    
    add_filter( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
    
  4. Realising this is a bit of an old thread, however I was stuck on a very similar problem, and thought I would share what I found which proved to be a very simple solution.

    <?php
    add_filter('manage_users_columns', 'pippin_add_user_id_column');
    function pippin_add_user_id_column($columns) {
        $columns['user_id'] = 'User ID';
        return $columns;
    }    
    
    add_action('manage_users_custom_column',  'pippin_show_user_id_column_content', 10, 3);
    function pippin_show_user_id_column_content($value, $column_name, $user_id) {
        $user = get_userdata( $user_id );
        if ( 'user_id' == $column_name )
            return $user_id;
        return $value;
    }
    ?>
    

    Credit: https://pippinsplugins.com/add-user-id-column-to-the-wordpress-users-table/

  5. To make the additional woocommerce fields editable you can use the following filter (in the example a custom field is added in the billing section).

    add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
        function add_woocommerce_customer_meta_fields($fields)
        {
          if (isset($fields['billing']['fields'])) {
            $fields['billing']['fields']['your_custom_meta'] = array(
              'label' => __('Friendly name', 'woocommerce'),
              'description' => ''
            );
          }
          return $fields;
        }
    

Comments are closed.