Add a new column to the admin Memberships table

If you've added some custom membership meta you may want to add a new column to the admin Memberships table to display that value. This can be done in two steps:

  1. Register your new column name.
  2. Display the column value for each membership.

This tutorial will walk you through these two steps using some custom membership meta for "Company".

1. Register the column name

This adds the column to the table, just without any values. For this we use the rcp_memberships_list_table_columns filter.

function ag_rcp_memberships_list_table_columns( $columns ) {

	$key = 'company'; // All lowercase, no spaces. Your unique ID for your column.
	$name = __( 'Company', 'rcp' ); // Label to be displayed on the column header. Spaces are okay.

	$columns[ $key ] = $name;

	return $columns;

}
add_filter( 'rcp_memberships_list_table_columns', 'ag_rcp_memberships_list_table_columns' );

The $key value needs to be changed. This should be all lowercase and use underscores instead of spaces (or no spaces).

The $name value needs to be changed. This is the label that is displayed in the column header. Spaces are okay here.

Once configured, the column will appear on the table, like so:

2. Display the column value

The next step is to populate the column value for each membership record. For this we use the rcp_memberships_list_table_column_{column} filter.

function ag_rcp_memberships_list_table_column_company( $value, $membership ) {

	$company = rcp_get_membership_meta( $membership->get_id(), 'company', true );

	return $company;

}
add_filter( 'rcp_memberships_list_table_column_company', 'ag_rcp_memberships_list_table_column_company', 10, 2 );

Make sure you edit the rcp_memberships_list_table_column_company filter to match your column key that you set in step 1. The correct filter to use is rcp_memberships_list_table_column_{column_key} where {column_key} is the key you chose. In my case that's company, which is why my filter name is rcp_memberships_list_table_column_company.

Inside the function, we use the membership meta API to retrieve our desired value. You will need to edit this to return your desired value. In this example, we retrieve the membership meta 'company'.

In the end, the table looks like this:

3. Remove default columns

You can also use the rcp_memberships_list_table_columns filter to remove default columns. All you need to know is the key of the column you want to remove. The default keys are as follows:

  • customer - Customer column
  • object_id - Membership level name column
  • status - Status column
  • auto_renew - Recurring column
  • created_date - Created column
  • expiration_date - Expiration date column

For example, this code can be used to remove the created date column:

function ag_rcp_memberships_list_remove_column( $columns ) {
	$key_to_remove = 'created_date'; // Change this to the key you want to remove.

	if ( array_key_exists( $key_to_remove, $columns ) ) {
		unset( $columns[ $key_to_remove] );
	}

	return $columns;
}
add_filter( 'rcp_memberships_list_table_columns', 'ag_rcp_memberships_list_remove_column' );