Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Limit the number of total sign ups for a given membership level
<?php
/*
Set a maximum number of members allowed to register for a membership level.
Add this code to a plugin for PMPro Customizations.
Set the "Maximum" for a level on the Memberships > Membership Levels > Edit Level admin page.
*/
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( isset( $_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'] );
//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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.