Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save strangerstudios/8721429685f3c9e729f21ea0a55be851 to your computer and use it in GitHub Desktop.
Save strangerstudios/8721429685f3c9e729f21ea0a55be851 to your computer and use it in GitHub Desktop.
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' );
@gausam
Copy link

gausam commented Aug 6, 2020

To disable the check when a level has an empty value for the limit, modify the pmproml_pmpro_registration_checks function to become:

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 );

	if ( empty( $limit ) ) {
		return $value;
	}
	
	//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;
}

@kimwhite
Copy link

@pabloc97
Copy link

Hi, i have this code installed using wp plugin: code snippets to get the php working as it do, the issue come when it appear on pmpro level settings but it doesnt appear when try to buy or similar. what can i do?

@laurenhagan0306
Copy link

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/

@plxmas
Copy link

plxmas commented Aug 16, 2021

I open pmpro-customizations.php and add this code but, after I set the limit in Memberships > Membership Levels > Edit Level admin page doesn't show “2/10 spots available” on page.

@plxmas
Copy link

plxmas commented Aug 16, 2021

I open pmpro-customizations.php and add this code but, after I set the limit in Memberships > Membership Levels > Edit Level admin page doesn't show “2/10 spots available” on page.

I added also

/*
 * This will show '0/X spots available.' on membership level description 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( $description, $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 default description for that level.
	if( empty( $limit ) ){
		return $description;
	}
	
	//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);

	$description .= $member_count . '/' . $limit;
	$description .= ' spots available.';

	return $description;

}
add_filter( 'pmpro_level_description', 'pmpro_show_spots_available', 10, 2 );

@bethgee
Copy link

bethgee commented Nov 28, 2021

I have added the above code, set the limit to one on a certain membership level and then did a test purchase. However, after doing so (ie this membership should no longer be available), the front end is still showing the signup form - can someone help me understand how to remove the signup form on the front end when memberships are no longer available?

@sradhaws
Copy link

Can we disable the Free membership plan "select" button from already purchased users?

@bethgee
Copy link

bethgee commented Jul 3, 2023

Is it possible to make the below into a shortcode so I can add it to a personalised levels page I have created??

I open pmpro-customizations.php and add this code but, after I set the limit in Memberships > Membership Levels > Edit Level admin page doesn't show “2/10 spots available” on page.

I added also

/*
 * This will show '0/X spots available.' on membership level description 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( $description, $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 default description for that level.
	if( empty( $limit ) ){
		return $description;
	}
	
	//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);

	$description .= $member_count . '/' . $limit;
	$description .= ' spots available.';

	return $description;

}
add_filter( 'pmpro_level_description', 'pmpro_show_spots_available', 10, 2 );

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