Home / Archive / MemberPress: Add Custom Field to Transactions Table
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Add Custom Field to Transactions Table

This code snippet will add data collected through a custom field on MemberPress registration forms to the transactions table at Dashboard > MemberPress > Transactions. 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 above 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 col_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['col_business_name'] = __('Business Name', 'memberpress');

To do this, Replace the col_business_name with the name of the actual custom field key with the col_prefix (e.g. col_your_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 col_business_name with the same field key used above (e.g. col_your_key), and adjust the business_name to match the key used (e.g your_key). Apply these changes on the following line:

$cols['col_business_name'] = array('business_name', true);

Next, replace col_business_name with your field key on this line also:

if($column_name == 'col_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_transaction_col($cols) {
$cols['col_business_name'] = __('Business Name', 'memberpress');
return $cols;
}
add_filter('mepr-admin-transactions-cols', 'custom_admin_transaction_col');
function custom_admin_transaction_col_sort($cols) {
$cols['col_business_name'] = array('business_name', true);
return $cols;
}
add_filter('mepr-admin-transactions-sortable-cols', 'custom_admin_transaction_col_sort');
function custom_admin_transaction_col_content($column_name, $rec, $attributes) {
if($column_name == 'col_business_name') {
  $user = get_user_by('login', $rec->user_login);
  $business_name = get_user_meta($user->ID, 'mepr_business_name', true);
} 
  ?>
  <td <?php echo esc_attr($attributes); ?>><?php echo esc_html($business_name); ?></td>
  <?php
}
add_filter('mepr-admin-transactions-cell', 'custom_admin_transaction_col_content', 10, 3);

Comments

Add a Comment