Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/0cf04f204087693666c8bac2c7412702 to your computer and use it in GitHub Desktop.
Save kimwhite/0cf04f204087693666c8bac2c7412702 to your computer and use it in GitHub Desktop.
Limit the number of total sign ups for a given membership level
<?php
/*
* This gist will add a field to set a maximum number of members allowed to register for a membership level.
* Set the "Maximum" for a level on the Memberships > Membership Levels > Edit Level admin page.
* 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/
*/
function pmproml_pmpro_save_membership_level( $level_id) {
if( $level_id <= 0 ) {
return;
}
$limit = $_REQUEST['pmpro_member_limit'];
update_option('pmpro_limit_'.$level_id, $limit);
}
add_action( 'pmpro_save_membership_level', 'pmproml_pmpro_save_membership_level' );
function pmproml_pmpro_membership_level_after_other_settings ( ) {
?>
<h3 class="topborder"><?php _e('Membership Limits', 'paid-memberships-pro');?></h3>
<table class="form-table">
<tbody>
<tr>
<th scope="row" valign="top"><label for="pmpro_member_limit"><?php _e('Maximum', 'paid-memberships-pro'); ?></label></th>
<td>
<?php
if( !empty( $_REQUEST['edit'] ) ) {
$edit = intval( $_REQUEST['edit'] );
$limit = get_option( 'pmpro_limit_' . $edit );
} else {
$limit = "";
}
?>
<input type="text" name="pmpro_member_limit" id="pmpro_member_limit" size="6" value="<?php echo $limit; ?>" />
<p class="description"><?php _e('Set the maximum number of members for this level.', 'paid-memberships-pro'); ?></p>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action( 'pmpro_membership_level_after_other_settings', 'pmproml_pmpro_membership_level_after_other_settings' );
function pmproml_pmpro_registration_checks( $value ) {
global $wpdb;
$level_id = intval( $_REQUEST['level'] );
//work if no amount is set.
if ( empty( $limit ) ) {
return $value;
}
//get the maximum number of members allowed in this level
$limit = get_option( 'pmpro_limit_' . $level_id );
//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);
//compare the count of members to the maximum number of members allowed in this level
if($member_count >= $limit) {
global $pmpro_msg, $pmpro_msgt;
$pmpro_msg = __('Membership limit has been reached for this level', 'paid-memberships-pro');
$pmpro_msgt = "pmpro_error";
$value = false;
}
return $value;
}
add_filter( 'pmpro_registration_checks', 'pmproml_pmpro_registration_checks' );
@daviddietz-zz
Copy link

I see that this code adds the option in the Member Level admin section, but how does one go about showing the total available slots on the front end?

@kimwhite
Copy link
Author

kimwhite commented Nov 6, 2021

I think the companion gist should add this on the LEVELS page for you https://gist.github.com/kimcoleman/51c03c3ec77ab84713dc80a512225f11

@daviddietz-zz
Copy link

I think the companion gist should add this on the LEVELS page for you https://gist.github.com/kimcoleman/51c03c3ec77ab84713dc80a512225f11

That's exactly what I was looking for! Thank you.

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