Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimcoleman/51c03c3ec77ab84713dc80a512225f11 to your computer and use it in GitHub Desktop.
Save kimcoleman/51c03c3ec77ab84713dc80a512225f11 to your computer and use it in GitHub Desktop.
Show number of spots available for a membership level Paid Memberships Pro.
<?php
/**
* This will show '0/X spots available.' on membership level if a limit is set from (https://www.paidmembershipspro.com/limit-number-members-membership-level/)
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* For help, post a support thread on www.paidmembershipspro.com
*/
function pmpro_show_spots_available( $expiration_text, $level ) {
global $wpdb;
$level_id = intval( $level->id );
// Get the maximum number of members allowed in this level.
$limit = get_option( 'pmpro_limit_' . $level_id );
// If the limit is not set, just return the expiration, if available, for that level.
if( empty( $limit ) ){
return $expiration_text;
}
// Get the count of members in this level.
$sql = "SELECT COUNT(*)
FROM {$wpdb->pmpro_memberships_users}
WHERE `status` LIKE 'active' AND `membership_id` = ". esc_sql($level_id);
$member_count = $wpdb->get_var($sql);
$expiration_text .= '<p class="pmpro-spots-available">';
$expiration_text .= $member_count . '/' . $limit;
$expiration_text .= ' spots available.';
$expiration_text .= '</p>';
return $expiration_text;
}
add_filter( 'pmpro_levels_expiration_text', 'pmpro_show_spots_available', 10, 2 );
@mdun234
Copy link

mdun234 commented Dec 12, 2023

This seems to show the number of spots FILLED out of number available, instead of just the number available... Not sure what to fix to adjust that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment