Created
April 9, 2025 08:26
-
-
Save ipokkel/44eaf49582e0e3d87e9327aaa7469a1c to your computer and use it in GitHub Desktop.
Add an "All Memberships Levels" column to the members list and include it in the CSV export.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds "All Membership Levels" column to the Members List and export CSV. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
// Adds "All Membership Levels" column to the Members List. | |
function my_pmpro_add_all_membership_levels_column( $columns ) { | |
$columns['all_membership_levels'] = 'All Membership Levels'; | |
return $columns; | |
} | |
add_filter( 'pmpro_manage_memberslist_columns', 'my_pmpro_add_all_membership_levels_column' ); | |
// Populate the "All Membership Levels" column in the Members List. | |
function my_pmpro_fill_all_membership_levels_column( $column_name, $user_id ) { | |
if ( 'all_membership_levels' === $column_name ) { | |
$levels = pmpro_getMembershipLevelsForUser( $user_id ); | |
if ( ! empty( $levels ) ) { | |
$level_names = array(); | |
foreach ( $levels as $level ) { | |
$level_names[] = $level->name; | |
} | |
echo esc_html( implode( ', ', $level_names ) ); | |
} else { | |
echo '—'; | |
} | |
} | |
} | |
add_action( 'pmpro_manage_memberslist_custom_column', 'my_pmpro_fill_all_membership_levels_column', 10, 2 ); | |
// Add the "All Membership Levels" column to the Members List CSV export. | |
function my_pmpro_add_all_membership_levels_to_csv( $columns ) { | |
$columns['all_membership_levels'] = 'my_pmpro_get_all_membership_levels_for_csv'; | |
return $columns; | |
} | |
add_filter( 'pmpro_members_list_csv_extra_columns', 'my_pmpro_add_all_membership_levels_to_csv' ); | |
// Populate the "All Membership Levels" column in the CSV export. | |
function my_pmpro_get_all_membership_levels_for_csv( $user, $column_name ) { | |
if ( 'all_membership_levels' === $column_name ) { | |
$levels = pmpro_getMembershipLevelsForUser( $user->ID ); | |
if ( ! empty( $levels ) ) { | |
$level_names = array(); | |
foreach ( $levels as $level ) { | |
$level_names[] = $level->name; | |
} | |
return implode( ', ', $level_names ); | |
} | |
} | |
return ''; | |
} | |
add_filter( 'pmpro_members_list_csv_extra_columns_data', 'my_pmpro_get_all_membership_levels_for_csv', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment