Forked from andrewlimaza/disable_double_checkout.php
Last active
August 4, 2025 11:14
-
-
Save dwanjuki/c5b1bf48aa8aefb5e00b9158beb96a06 to your computer and use it in GitHub Desktop.
Do not allow membership renewal unless the level is expiring in the next 45 days.
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 | |
| /** | |
| * Do not allow membership renewal unless the level is expiring in the next 45 days. | |
| * | |
| * 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. | |
| * Read this companion article for step-by-step directions on either method. | |
| * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
| */ | |
| /** | |
| * Consider a level to be expiring soon only if there's 45 days or less until expiration. | |
| */ | |
| function my_pmpro_is_level_expiring_soon( $r, $level ) { | |
| if ( ! empty( $level->enddate ) ) { | |
| $now = time(); | |
| $expiration_date = $level->enddate; | |
| $days_until_expiration = ceil( ( $expiration_date - $now ) / ( 60 * 60 * 24 ) ); | |
| if ( $days_until_expiration > 45 ) { | |
| $r = false; | |
| } else { | |
| $r = true; | |
| } | |
| } | |
| return $r; | |
| } | |
| add_filter( 'pmpro_is_level_expiring_soon', 'my_pmpro_is_level_expiring_soon', 10, 2 ); | |
| /** | |
| * Redirect from Checkout page to the Levels page if the level is not expiring soon. | |
| */ | |
| function my_pmpro_checkout_redirect_non_expiring_soon_levels() { | |
| // Make sure PMPro is active and that we are on a checkout page. | |
| if ( ! function_exists( 'pmpro_is_checkout' ) || ! pmpro_is_checkout() ) { | |
| return; | |
| } | |
| // Logged in users only. | |
| $user_id = get_current_user_id(); | |
| if ( empty( $user_id ) ) { | |
| return; | |
| } | |
| // Get checkout level. | |
| $pmpro_level = pmpro_getLevelAtCheckout(); | |
| // Bail if the user does not have the checkout level. | |
| if ( ! pmpro_hasMembershipLevel( $pmpro_level->id ) ) { | |
| return; | |
| } | |
| // The user has the checkout level. Bail if it's expiring soon. | |
| $level = pmpro_getSpecificMembershipLevelForUser( $user_id, $pmpro_level->id ); | |
| if ( pmpro_isLevelExpiringSoon( $level ) ) { | |
| return; | |
| } | |
| // Redirect to the levels page. Adding a query parameter so we can show a message on the levels page. | |
| wp_safe_redirect( add_query_arg( 'norenewal', '1', pmpro_url( 'levels' ) ) ); | |
| exit; | |
| } | |
| add_action( 'template_redirect', 'my_pmpro_checkout_redirect_non_expiring_soon_levels' ); | |
| /** | |
| * Show a message on the levels page. | |
| */ | |
| function the_content_add_non_renewal_message( $content ) { | |
| global $pmpro_pages; | |
| if ( ! empty( $pmpro_pages ) && is_page( $pmpro_pages['levels'] ) && ! empty( $_REQUEST['norenewal'] ) ) { | |
| $content = '<p><strong>You must wait before renewing your membership.</strong></p>' . $content; | |
| } | |
| return $content; | |
| } | |
| add_filter( 'the_content', 'the_content_add_non_renewal_message' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment