Home / Admin / MemberPress: Add Custom Field Date to Members Table
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Add Custom Field Date to Members Table

This code snippet will add data collected through a custom field on MemberPress registration forms to the members' table at Dashboard > MemberPress > Members. The data will be added to the new custom column.

To use this code snippet, a custom field should be created at Dashboard > MemberPress > Settings > Fields tab.

The example code adds the data collected using the custom field named Business Name. The slug of this custom MemberPress field is mepr_business_name. The custom field key used is business_name.

To modify the code and adjust it to any custom field, the field name and field key should be replaced on this line:

$cols['business_name'] = __('Business Name', 'memberpress');

To do this, Replace the business_name with the name of the actual custom field key. Next, replace the dummy “Business Name” text with the name of the actual custom field, or any other organizational name that should be used for this column.

Further, replace business_name with the same field key used above, on this line:

if($column_name == 'business_name') {

Finally, replace the mepr_business_name slug with the slug of the actual custom field, on this line:

$business_name = get_user_meta($user->ID, 'mepr_business_name', true);

Code Preview
php
<?php
function custom_admin_members_col($cols) {
  $cols['business_name'] = __('Business Name', 'memberpress'); // Replace the business_name with the name of the actual custom field key & replace the dummy Business Name text with the name of the actual custom field 
  return $cols;
}
add_filter('mepr-admin-members-cols', 'custom_admin_members_col');
function custom_admin_members_col_content($attributes, $rec, $column_name, $column_display_name) {
  if($column_name == 'business_name') { // Replace the business_name with the actual custom field key
    $user = get_user_by('login', $rec->username);
    $business_name = get_user_meta($user->ID, 'mepr_business_name', true); // Replace the dummy mepr_business_name slug with the actual custom field slug
  ?>
    <td <?php echo $attributes; ?>><?php echo $business_name; ?></td>
  <?php
  }
}
add_filter('mepr_members_list_table_row', 'custom_admin_members_col_content', 10, 4);
add_action('admin_head', function() {
    echo '<style>
            th#business_name {
                width: 100px;
            }
        </style>';
});

Comments

Add a Comment