Forked from kimcoleman/pmpro_show_spots_available.php
Last active
April 16, 2024 09:26
-
-
Save dwanjuki/7e2212e5d7bc0db77bd77ca5ae3dbe1a to your computer and use it in GitHub Desktop.
Show number of spots available for a membership level Paid Memberships Pro.
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 | |
/** | |
* 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 taken.'; | |
$expiration_text .= '</p>'; | |
return $expiration_text; | |
} | |
add_filter( 'pmpro_levels_expiration_text', 'pmpro_show_spots_available', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment