Skip to content

Instantly share code, notes, and snippets.

@cartpauj
Last active April 28, 2023 09:08
Show Gist options
  • Save cartpauj/fec536f652794a01fc61dc55a2b83c31 to your computer and use it in GitHub Desktop.
Save cartpauj/fec536f652794a01fc61dc55a2b83c31 to your computer and use it in GitHub Desktop.
Limit # of signups on MemberPress memberships
<?php
//Sample code showing how to limit the total # of signups allowed on two different memberships
//Also some code at the bottom to show a custom message on the signup page when limit is reached
//Used to check if a signup limit has been reached for a particular membership
function has_reached_limit($membership_id, $limit) {
global $wpdb;
$query = "SELECT count(DISTINCT user_id)
FROM {$wpdb->prefix}mepr_transactions
WHERE status IN('complete', 'confirmed')
AND (
expires_at IS NULL
OR expires_at = '0000-00-00 00:00:00'
OR expires_at >= NOW()
)
AND product_id = {$membership_id}";
$count = $wpdb->get_var($query);
return ($count >= $limit);
}
//Limit membership one
function limit_signups_for_membership_one($errors) {
//CHANGE THE FOLLOWING TWO VARS
$membership_id = 123; //The Product you want to limits' ID
$limit = 10; //Number of signups allowed
if($_POST['mepr_product_id'] != $membership_id) { return $errors; }
if(has_reached_limit($membership_id, $limit)) {
$errors[] = __('Sorry, our signup limit of ' . $limit . ' members has been reached. No further signups are allowed.', 'memberpress');
}
return $errors;
}
add_filter('mepr-validate-signup', 'limit_signups_for_membership_one');
//Limit membership two
function limit_signups_for_membership_two($errors) {
//CHANGE THE FOLLOWING TWO VARS
$membership_id = 789; //The Product you want to limits' ID
$limit = 25; //Number of signups allowed
if($_POST['mepr_product_id'] != $membership_id) { return $errors; }
if(has_reached_limit($membership_id, $limit)) {
$errors[] = __('Sorry, our signup limit of ' . $limit . ' members has been reached. No further signups are allowed.', 'memberpress');
}
return $errors;
}
add_filter('mepr-validate-signup', 'limit_signups_for_membership_two');
//Override message for membersihp one
function maybe_override_membership_signup_form_one($content) {
global $post;
//CHANGE THE FOLLOWING TWO VARS
$membership_id = 123; //The Product you want to limits' ID
$limit = 10; //Number of signups allowed
if( isset($post) && $post instanceof WP_Post &&
$post->id == $membership_id &&
has_reached_limit($membership_id, $limit) ) {
ob_start();
?>
<div id="registration_full">
<p>I'm sorry, our registration has reached its capacity of <?php echo $limit; ?>. Please check back for xyz.</p>
</div>
<?php
$content = ob_get_clean();
}
return $content;
}
add_filter('the_content', 'maybe_override_membership_signup_form_one', 9999999, 1);
//Override message for membersihp two
function maybe_override_membership_signup_form_two($content) {
global $post;
//CHANGE THE FOLLOWING TWO VARS
$membership_id = 789; //The Product you want to limits' ID
$limit = 25; //Number of signups allowed
if( isset($post) && $post instanceof WP_Post &&
$post->id == $membership_id &&
has_reached_limit($membership_id, $limit) ) {
ob_start();
?>
<div id="registration_full">
<p>I'm sorry, our registration has reached its capacity of <?php echo $limit; ?>. Please check back for xyz.</p>
</div>
<?php
$content = ob_get_clean();
}
return $content;
}
add_filter('the_content', 'maybe_override_membership_signup_form_two', 9999999, 1);
@cartpauj
Copy link
Author

FYI - there's now a 3rd party plugin that does this without any coding necessary: https://meprtoolbox.com/product/limit-membership-signups/

@ieltstestonline
Copy link

Hi Cartpauj - new to Github (created an account so I could send this message!) so apologies if I'm going about this the wrong way, but do you do commissioned work? I am trying to get this code to work for multiple memberships (or for a single group). Is this something you offer?

Thanks!

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