Created
December 12, 2025 16:48
-
-
Save chrisegg/1cce636027b371509ad0f22b8389071a to your computer and use it in GitHub Desktop.
Automatically calculate the correct new expiration date based on product term and current membership term and status
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 | |
| /** | |
| * Membership Renewal Calculation | |
| * | |
| * When a logged-in user submits the membership renewal form (ID 1), automatically calculate the correct new expiration date, | |
| * extending from their current expiration if active, or from today if expired, based on whether they chose a 1, 2, or 3-year | |
| * renewal, and save it to their user profile. | |
| * | |
| * Usage: Change the form and field ID numbers to match those used in your form. | |
| * | |
| * Author: Chris Eggleston | |
| * Version: 1.0 | |
| * | |
| * | |
| */ | |
| add_action( 'gform_pre_submission_1', function( $form ) { | |
| // ----------------------------------------------------- | |
| // 1. Get the selected renewal product option | |
| // ----------------------------------------------------- | |
| $selected_value = rgpost('input_10'); | |
| if ( ! $selected_value ) { | |
| return $form; | |
| } | |
| // ----------------------------------------------------- | |
| // 2. Determine membership duration (1, 2, 3 years) | |
| // ----------------------------------------------------- | |
| $years = 0; | |
| if ( stripos( $selected_value, '1 Year' ) !== false ) { | |
| $years = 1; | |
| } elseif ( stripos( $selected_value, '2 Years' ) !== false ) { | |
| $years = 2; | |
| } elseif ( stripos( $selected_value, '3 Years' ) !== false ) { | |
| $years = 3; | |
| } | |
| if ( $years === 0 ) { | |
| return $form; | |
| } | |
| // ----------------------------------------------------- | |
| // 3. Get current expiration from user meta | |
| // ----------------------------------------------------- | |
| $user_id = get_current_user_id(); | |
| $current_expiration = get_user_meta( $user_id, 'membership_expiration', true ); | |
| $today = new DateTime(); | |
| $base_date = null; | |
| // ----------------------------------------------------- | |
| // 4. Determine the starting point for the renewal | |
| // ----------------------------------------------------- | |
| if ( $current_expiration ) { | |
| try { | |
| $expiration_dt = DateTime::createFromFormat( 'm/d/Y', $current_expiration ); | |
| if ( $expiration_dt && $expiration_dt >= $today ) { | |
| // ACTIVE MEMBERSHIP — extend from expiration date | |
| $base_date = $expiration_dt; | |
| } else { | |
| // EXPIRED — renew from today | |
| $base_date = $today; | |
| } | |
| } catch ( Exception $e ) { | |
| // If corrupted value, renew from today | |
| $base_date = $today; | |
| } | |
| } else { | |
| // No previous expiration found — new membership or data missing | |
| $base_date = $today; | |
| } | |
| // ----------------------------------------------------- | |
| // 5. Apply membership extension | |
| // ----------------------------------------------------- | |
| $base_date->modify( '+' . $years . ' year' ); | |
| $new_expiration = $base_date->format('m/d/Y'); | |
| // ----------------------------------------------------- | |
| // 6. Store in hidden expiration field 16 | |
| // User Registration Add-on will save to usermeta | |
| // ----------------------------------------------------- | |
| $_POST['input_16'] = $new_expiration; | |
| return $form; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment