Forked from andrewlimaza/pmpro_show_spots_available.php
Last active
April 12, 2024 06:54
-
-
Save kimcoleman/51c03c3ec77ab84713dc80a512225f11 to your computer and use it in GitHub Desktop.
Show number of spots available for a membership level Paid Memberships Pro.
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 | |
/** | |
* 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 ); |
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
This recipe is included in the blog post on "Limit the Number of Members by Membership Level" at Paid Memberships Pro here: https://www.paidmembershipspro.com/limit-number-members-membership-level/