Last active
December 12, 2025 17:02
-
-
Save chrisegg/fb427d348d744bcffd1fe68ba584c600 to your computer and use it in GitHub Desktop.
Automatically calculate the correct expiration date based on product term for new members.
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 Expiration Date Calculation | |
| * | |
| * When someone submits Form 7 and picks a 1-, 2-, or 3-year membership option (field 10), this code creates a brand-new | |
| * membership that starts today and expires exactly that many years in the future, then puts the expiration date into hidden | |
| * field 16 so it gets saved to the user’s 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_7', function( $form ) { | |
| // ----------------------------------------------------- | |
| // 1. Get the raw product field value (full label|price) | |
| // ----------------------------------------------------- | |
| $selected_value = rgpost('input_10'); | |
| if ( ! $selected_value ) { | |
| return $form; // No membership selected | |
| } | |
| // ----------------------------------------------------- | |
| // 2. Determine membership duration (1, 2, or 3 years) | |
| // ----------------------------------------------------- | |
| $years = 0; | |
| // Match against the label text | |
| 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 nothing matched, do not populate expiration | |
| if ( $years === 0 ) { | |
| return $form; | |
| } | |
| // ----------------------------------------------------- | |
| // 3. Calculate expiration date | |
| // ----------------------------------------------------- | |
| $start = new DateTime(); // today's date | |
| $start->modify( '+' . $years . ' year' ); // add membership period | |
| $expiration_date = $start->format('m/d/Y'); // save format: MM/DD/YYYY | |
| // ----------------------------------------------------- | |
| // 4. Store the expiration date in hidden field 16 | |
| // ----------------------------------------------------- | |
| $_POST['input_16'] = $expiration_date; | |
| return $form; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment