Created
September 26, 2024 12:50
-
-
Save dwanjuki/fdf8fb7cd7cd0a65e7736a114ba7302d to your computer and use it in GitHub Desktop.
Add Membership Status to Users list
This file contains hidden or 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 | |
/* | |
* Add membership status to WordPress Users list | |
* | |
* Statuses: https://www.paidmembershipspro.com/documentation/advanced/pmpro-database-structure/#10 | |
* | |
* 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 'Membership Status' Column to Users List Header | |
function my_pmpro_membership_status_manage_users_columns( $columns ) { | |
if( function_exists( 'pmpro_getMembershipLevelsForUser' ) ) { | |
$columns['membership-status'] = 'Membership Status'; | |
} | |
return $columns; | |
} | |
add_filter( 'manage_users_columns', 'my_pmpro_membership_status_manage_users_columns' ); | |
// Add 'Membership Status' data to Users List Rows | |
function my_pmpro_membership_status_manage_users_custom_column( $column_data, $column_name, $user_id ) { | |
global $wpdb; | |
if($column_name == 'membership-status') { | |
$levels = pmpro_getMembershipLevelsForUser($user_id); | |
$level_statuses = array(); | |
if( ! empty( $levels ) ) { | |
foreach( $levels as $key => $level ) { | |
$status = $wpdb->get_var( "SELECT status FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user_id . "' AND membership_id = '" . $level->id . "' ORDER BY id DESC LIMIT 1" ); | |
$level_statuses[] = $status; | |
$column_data = implode( ', ', $level_statuses ); | |
} | |
} else { | |
$last_status = $wpdb->get_var( "SELECT status FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1" ); | |
$column_data = $last_status ?? 'none'; | |
} | |
} | |
return $column_data; | |
} | |
add_action( 'manage_users_custom_column', 'my_pmpro_membership_status_manage_users_custom_column', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment