Adding custom columns to CSV export

There are two steps to adding custom columns to a members or payments export:

  1. Add your column header
  2. Add the column data for each member or payment

Adding your column header

A custom column is added with the following filter: rcp_export_csv_cols_{$export_type}, where $export_type is either "members" (for a member export) or "payments" (for a payment export).

Here's an example for adding a new "Join Date" column to the members export:

/**
 * Add new export column for "Join Date".
 * 
 * @param array $columns Default column headers.
 * 
 * @return array
 */
function ag_rcp_members_export_join_date_header( $columns ) {
    $columns['join_date'] = __( 'Join Date' );

    return $columns;
}
add_filter( 'rcp_export_csv_cols_members', 'ag_rcp_members_export_join_date_header' );

Adding the column value for each row

The next step is to add the corresponding value for each member or payment.

Members

For members, this is done with the  rcp_export_members_get_data_row filter. Two parameters are passed in:

  1. $row (array) - An array of key/value pairs for the current member.
  2. $member (RCP_Member) - RCP_Member object for the current member being exported.

Here's an example for adding the join date for each member:

/**
 * Add the join date value for each member.
 * 
 * @param array      $row    Array of data to be exported for the current member.
 * @param RCP_Member $member Member object.
 * 
 * @return array
 */
function ag_rcp_members_export_join_date( $row, $member ) {
    $row['join_date'] = $member->get_joined_date();

    // Or you can add a piece of user meta like this:
    // $row['my_field'] = get_user_meta( $member->ID, 'my_custom_field', true );

    return $row;
}
add_filter( 'rcp_export_members_get_data_row', 'ag_rcp_members_export_join_date', 10, 2 );

Payments

For payments, this is done with the  rcp_export_payments_get_data_row filter. Two parameters are passed in:

  1. $row (array) - An array of key/value pairs for the current payment.
  2. $payment (object) - Payment object from the database. The payment ID can be retrieved with: $payment->id

Here's an example for adding the transaction ID for each payment (note: you also need to add the column header, as explained previously):

/**
 * Add the transaction ID value for each payment.
 * 
 * @param array  $row     Array of data to be exported for the current payment.
 * @param object $payment Payment object from the database.
 * 
 * @return array
 */
function ag_rcp_payments_export_transaction_id( $row, $payment ) {
    $row['transaction_id'] = $payment->transaction_id;

    return $row;
}
add_filter( 'rcp_export_payments_get_data_row', 'ag_rcp_payments_export_transaction_id', 10, 2 );