Created
February 13, 2026 02:41
-
-
Save davidmutero/40a8d96dc33c9affeef7c86d84279791 to your computer and use it in GitHub Desktop.
Restrict Email Adress When Using a Specific Discount Code at Checkout By Level
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 | |
| /** | |
| * Require a .edu email address when using a specific discount code at checkout. | |
| * | |
| * This recipe checks for a discount code applied via URL or checkout, | |
| * and blocks checkout if the billing email address does not end in .edu. | |
| * | |
| * You can optionally restrict this rule to specific membership levels | |
| * and/or specific discount codes. | |
| * | |
| * 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. | |
| * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
| */ | |
| /** | |
| * Validate that the billing email ends in .edu when a restricted discount code is used. | |
| * | |
| * @param bool $continue Whether checkout validation should continue. | |
| * @return bool True to continue checkout, false to block checkout. | |
| */ | |
| function my_pmpro_require_edu_for_url_discount_code( $continue ) { | |
| // Only run if PMPro checkout level function exists. | |
| if ( ! function_exists( 'pmpro_getLevelAtCheckout' ) ) { | |
| return $continue; | |
| } | |
| $level = pmpro_getLevelAtCheckout(); | |
| if ( empty( $level ) || empty( $level->id ) ) { | |
| return $continue; | |
| } | |
| /** | |
| * OPTIONAL: Restrict this validation to specific membership levels. | |
| * | |
| * Update the array below with the level IDs you want to target. | |
| * Leave empty to apply to all levels. | |
| */ | |
| $restricted_levels = array( 7 ); // UPDATE: Add/remove level IDs as needed. | |
| if ( ! empty( $restricted_levels ) && ! in_array( intval( $level->id ), $restricted_levels, true ) ) { | |
| return $continue; | |
| } | |
| // Grab discount code from URL if present. | |
| $url_code = ''; | |
| if ( ! empty( $_GET['pmpro_discount_code'] ) ) { | |
| $url_code = strtoupper( sanitize_text_field( wp_unslash( $_GET['pmpro_discount_code'] ) ) ); | |
| } | |
| // Check if PMPro already loaded a discount code onto the level object. | |
| $level_code = ''; | |
| if ( ! empty( $level->discount_code ) ) { | |
| $level_code = strtoupper( sanitize_text_field( $level->discount_code ) ); | |
| } | |
| // Use whichever discount code we have available. | |
| $code = $url_code ? $url_code : $level_code; | |
| // Bail if no discount code is being used. | |
| if ( empty( $code ) ) { | |
| return $continue; | |
| } | |
| /** | |
| * OPTIONAL: Restrict this rule to specific discount codes. | |
| * | |
| * Update the array below with the discount codes you want to target. | |
| * Leave empty to apply to all discount codes. | |
| */ | |
| $restricted_codes = array( 'STUDENT10' ); // UPDATE: Add/remove codes as needed. | |
| if ( ! empty( $restricted_codes ) && ! in_array( $code, $restricted_codes, true ) ) { | |
| return $continue; | |
| } | |
| // Bail if email is missing. Default PMPro validation will handle required email fields. | |
| if ( empty( $_REQUEST['bemail'] ) ) { | |
| return $continue; | |
| } | |
| $email = sanitize_email( wp_unslash( $_REQUEST['bemail'] ) ); | |
| // Require .edu email address. | |
| if ( ! preg_match( '/\.edu$/i', $email ) ) { | |
| pmpro_setMessage( 'This discount code is only valid for members using a .edu email address.', 'pmpro_error' ); | |
| return false; | |
| } | |
| return $continue; | |
| } | |
| add_filter( 'pmpro_registration_checks', 'my_pmpro_require_edu_for_url_discount_code', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment