Skip to content

Instantly share code, notes, and snippets.

@czachor
Forked from magnific0/wp_usermeta.md
Last active June 30, 2021 08:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czachor/da76665b9a00294f8ab82fe10979b528 to your computer and use it in GitHub Desktop.
Save czachor/da76665b9a00294f8ab82fe10979b528 to your computer and use it in GitHub Desktop.
Show and Edit User Meta in Wordpress

Show and Edit User Meta in WordPress (works with UltimateMember)

Description

This simple procedure will allow you to:

  1. Display user meta fields under in the user list as additional columns (Users > All Users).
  2. Display these fields on user profiles.
  3. Edit these fields under user edit.

This method works completely without plugins and involves just some functions and hooks in functions.php. Plugins like "User Meta Display" achieve this to some level, but treat custom meta fiedlds completely different from the regular fields. They are shown and edited in seperate environment and fail to show the meta data is a table list. This method integrates custom user meta along with regular user (meta).

Fork changes

  1. get_user_meta() instead of get_the_author_meta() - the last one adds user_ prefix to some fields (eg. "status" -> "user_status"), which leads to getting improper values
  2. update_user_meta() instead of deprecated update_usermeta()
  3. Code style follows PSR (sry, don't like WP style)
  4. Code optimizations

Implementation

First one needs to set-up which meta fields are important, it was chosen to go for a modular set-up with an array. This way the method is flexible and easy for large number of fields. The key element to the array entry is the meta field name, whereas the value gives a nice printable name.

// list of meta keys
function mysite_custom_define() {
    $custom_meta_fields = [];
    // key = name to show
    $custom_meta_fields['twitter'] = 'Twitter handle';
    $custom_meta_fields['linkedin'] = 'LinkedIn page';
    $custom_meta_fields['facebook'] = 'Facebook profile';

    return $custom_meta_fields;
}

The next two functions create the columns and fill them respectively:

function mysite_columns($defaults) {
    $meta_number = 0;
    $custom_meta_fields = mysite_custom_define();

    foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        $meta_number++;
        $defaults['mysite-usercolumn-' . $meta_number] = __($meta_disp_name, 'user-column');
    }

  return $defaults;
}

function mysite_custom_columns($value, $column_name, $id) {
    $meta_number = 0;
    $custom_meta_fields = mysite_custom_define();

    foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        $meta_number++;

        if ($column_name === 'mysite-usercolumn-' . $meta_number) {
            return get_user_meta($id, $meta_field_name, true);
        }
    }
    
    return '';
}

To show this same information on the user profile and edit pages, the following functions are added:

function mysite_show_extra_profile_fields($user) {
    echo '<h3>Extra profile information</h3>';
    echo '<table class="form-table">';

    $custom_meta_fields = mysite_custom_define();

    foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        echo '<tr>';
        echo '<th><label for="' . $meta_field_name . '">' . $meta_disp_name . '</label></th>';
        echo '<td>';
        echo '<input type="text" name="' . $meta_field_name . '" id="' . $meta_field_name . '" value="' . esc_attr(get_user_meta($user->ID, $meta_field_name, true)) . '" class="regular-text"><br>';
        echo '<span class="description"></span>';
        echo '</td>';
        echo '</tr>';
    }

    echo '</table>';
}

Displaying is one thing, however changes need to be saved, hence the last addition.

function mysite_save_extra_profile_fields($user_id) {
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }

    $custom_meta_fields = mysite_custom_define();

    foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        update_user_meta($user_id, $meta_field_name, $_POST[$meta_field_name]);
    }
    
    return true;
}

Lastly the functions are integrated in Wordpress through the followig handles.

    add_action('show_user_profile', 'mysite_show_extra_profile_fields');
    add_action('edit_user_profile', 'mysite_show_extra_profile_fields');
    add_action('personal_options_update', 'mysite_save_extra_profile_fields');
    add_action('edit_user_profile_update', 'mysite_save_extra_profile_fields');
    add_action('manage_users_custom_column', 'mysite_custom_columns', 15, 3);
    add_filter('manage_users_columns', 'mysite_columns', 15, 1);    

Sources

The following two posts provided the basis for this solution:

  1. http://wordpress.stackexchange.com/questions/5991/how-to-display-multiple-custom-columns-in-the-wp-admin-users-php
  2. http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
@hirenshah
Copy link

Would you happen to know how to add sorting to the custom columns?

@czachor
Copy link
Author

czachor commented Dec 12, 2019

Would you happen to know how to add sorting to the custom columns?

I'm sorry, I don't know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment