Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/6216ac043d116aca06b995af4baa3e7b to your computer and use it in GitHub Desktop.
Save ipokkel/6216ac043d116aca06b995af4baa3e7b to your computer and use it in GitHub Desktop.
<?php
/**
* Add a "Total Memberships" column to the Members List and Users List.
*
* 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/
*/
// Add 'Total Memberships' Column to Members columns.
function my_pmpro_add_memberslist_col_membership_count( $columns ) {
$columns['membership_count'] = 'Total Memberships';
return $columns;
}
add_filter( 'pmpro_manage_memberslist_columns', 'my_pmpro_add_memberslist_col_membership_count' );
/**
* Fills the "Total Memberships" column of the Members List.
*
* @param string $colname column being filled.
* @param string $user_id to get information for.
*/
function my_pmpro_fill_memberslist_col_membership_count( $colname, $user_id ) {
if ( 'membership_count' === $colname ) {
// get the number of all memberships for the user, including expired/cancelled ones.
$memberships = pmpro_getMembershipLevelsForUser( $user_id, true );
echo '<a href="' . esc_url( add_query_arg( array( 'page' => 'pmpro-member', 'user_id' => (int) $user_id, 'pmpro_member_edit_panel' => 'memberships' ), admin_url( 'admin.php' ) ) ) . '">' . count( $memberships ) . '</a>';
// echo esc_html( get_user_meta( $user_id, 'membership_count', true ) );
}
}
add_filter( 'pmpro_manage_memberslist_custom_column', 'my_pmpro_fill_memberslist_col_membership_count', 10, 2 );
//Add 'Total Memberships' Column to Users List Header
function my_pmpro_manage_users_columns( $columns ) {
$columns['membership_count'] = 'Total Memberships';
return $columns;
}
add_filter( 'manage_users_columns', 'my_pmpro_manage_users_columns' );
//Add 'Total Memberships' Column data to Users List Rows
function my_pmpro_manage_users_custom_column( $value, $column_name, $user_id ) {
// $theuser = get_userdata( $user_id );
if ( 'membership_count' == $column_name ) {
$memberships = pmpro_getMembershipLevelsForUser( $user_id, true );
return '<a href="' . esc_url( add_query_arg( array( 'page' => 'pmpro-member', 'user_id' => (int) $user_id, 'pmpro_member_edit_panel' => 'memberships' ), admin_url( 'admin.php' ) ) ) . '">' . count( $memberships ) . '</a>';
}
return $value;
}
add_action( 'manage_users_custom_column', 'my_pmpro_manage_users_custom_column', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment