Created
January 27, 2025 18:52
-
-
Save cartpauj/ff2401071d38d7cc3ccd40e551b5f1cc to your computer and use it in GitHub Desktop.
Require a coupon code to signup for a Free MemberPress Membership (does not work with ReadyLaunch templates)
This file contains hidden or 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 | |
// CHANGE this array - should be a comma separated list of Membership ID's to apply the signup codes to | |
$GLOBALS['free_memberships_require_coupon'] = array(2734); | |
function make_coupon_mandatory_free_membership($errors) { | |
if(!isset($_POST['mepr_coupon_code']) || trim($_POST['mepr_coupon_code']) == '' && in_array($_POST['mepr_product_id'], $GLOBALS['free_memberships_require_coupon'])) { | |
$errors[] = 'Sorry, a coupon code is required.'; | |
} | |
return $errors; | |
} | |
add_filter('mepr-validate-signup', 'make_coupon_mandatory_free_membership'); | |
// Needed to show hidden coupon field on free memberships | |
function show_coupon_field_free_membership() { | |
$is_free_membership = false; | |
foreach($GLOBALS['free_memberships_require_coupon'] as $membership_id) { | |
if(is_single($membership_id)) { | |
$is_free_membership = true; | |
} | |
} | |
if($is_free_membership): | |
?> | |
<script type="text/javascript"> | |
(function($) { | |
$(document).ready(function() { | |
// Not needed for free memberships as coupon field is hidden and no link to enable it | |
// Uncomment for non-free memberships though | |
// $('.mepr-signup-form .have-coupon-link').trigger('click'); | |
$('input[name="mepr_coupon_code"]').attr("type", "text"); | |
$('input[name="mepr_coupon_code"]').before('<label>Coupon Code (Required)*</label>'); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
endif; | |
} | |
add_action('wp_footer', 'show_coupon_field_free_membership'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment