Created
April 7, 2026 12:05
-
-
Save JarrydLong/39420c77e8c1034781b9e57f0b149a4f to your computer and use it in GitHub Desktop.
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 copy | |
| /** | |
| * Starting Year Selection at Checkout (using PMPro_Field class) | |
| * | |
| * Adds a "Starting Year" dropdown to the PMPro checkout page using the | |
| * native PMPro_Field class. The field is scoped to the billing level only. | |
| * | |
| * After checkout, a custom save_function reads the selected year, saves | |
| * it as user meta (used by the renewal progression recipe), and immediately | |
| * assigns the corresponding access level to the member. | |
| * | |
| * Designed to work alongside the renewal recipe: | |
| * https://gist.github.com/JarrydLong/4933a5649e753772cf5a114356bebd3d#file-mypmpro-add-new-level-for-every-year-of-renewal-php | |
| * | |
| * 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/ | |
| */ | |
| /** | |
| * CONFIGURATION — update these IDs to match your PMPro level IDs. | |
| */ | |
| function mypmpro_starting_year_config() { | |
| return array( | |
| 'billing_level_id' => 1, // The recurring billing level | |
| 'year_level_map' => array( | |
| 1 => 2, // Year 1 => Level ID | |
| 2 => 3, // Year 2 => Level ID | |
| 3 => 4, // Year 3 => Level ID | |
| 4 => 5, // Year 4 => Level ID | |
| ), | |
| ); | |
| } | |
| /** | |
| * Register the "Starting Year" field using the PMPro_Field class. | |
| * The field is shown only on the billing level's checkout page, | |
| * not on the user's profile (profile => false). | |
| */ | |
| function mypmpro_register_starting_year_field() { | |
| if ( ! class_exists( 'PMPro_Field' ) ) { | |
| return; | |
| } | |
| $config = mypmpro_starting_year_config(); | |
| // Add a labelled field group so the dropdown has its own section header. | |
| pmpro_add_field_group( 'starting_year_group', __( 'Programme Details', 'paid-memberships-pro' ) ); | |
| $field = new PMPro_Field( | |
| 'pmpro_starting_year', // name / user meta key | |
| 'select', // field type | |
| array( | |
| 'label' => __( 'Starting Year', 'paid-memberships-pro' ), | |
| 'hint' => __( "Select the year you would like to start from. Access to that year's content will be granted immediately after checkout.", 'paid-memberships-pro' ), | |
| 'required' => true, | |
| 'profile' => false, // only show at checkout, not on profile page | |
| 'levels' => array( $config['billing_level_id'] ), | |
| 'options' => array( | |
| '' => __( '— Please select —', 'paid-memberships-pro' ), | |
| '1' => __( 'Year 1', 'paid-memberships-pro' ), | |
| '2' => __( 'Year 2', 'paid-memberships-pro' ), | |
| '3' => __( 'Year 3', 'paid-memberships-pro' ), | |
| '4' => __( 'Year 4', 'paid-memberships-pro' ), | |
| ), | |
| 'save_function' => 'mypmpro_save_starting_year', | |
| ) | |
| ); | |
| pmpro_add_user_field( 'starting_year_group', $field ); | |
| } | |
| add_action( 'init', 'mypmpro_register_starting_year_field' ); | |
| /** | |
| * Custom save_function: called by PMPro after checkout and profile save. | |
| * | |
| * Persists the selected year as user meta (the renewal progression | |
| * recipe reads 'pmpro_starting_year' to know which year to start from), | |
| * then assigns the matching access level to the member. | |
| * | |
| * @param int $user_id The ID of the user being saved. | |
| * @param string $field The field name (meta key) being saved. | |
| */ | |
| function mypmpro_save_starting_year( $user_id, $field ) { | |
| $config = mypmpro_starting_year_config(); | |
| $posted_value = isset( $_REQUEST[ $field ] ) ? sanitize_text_field( $_REQUEST[ $field ] ) : ''; | |
| $starting_year = (int) $posted_value; | |
| // Only proceed with a valid year selection. | |
| if ( $starting_year < 1 || $starting_year > 4 ) { | |
| return; | |
| } | |
| // Save as user meta so the renewal recipe can use it. | |
| update_user_meta( $user_id, 'pmpro_starting_year', $starting_year ); | |
| /** | |
| * Assign only the chosen starting year's access level. | |
| * The renewal recipe will unlock subsequent years automatically | |
| * on each future annual payment. | |
| * | |
| * To instead grant ALL years up to the chosen one immediately | |
| * (e.g. Year 3 also unlocks Years 1 & 2), change the condition to: | |
| * for ( $y = 1; $y <= $starting_year; $y++ ) { ... } | |
| */ | |
| $level_id = isset( $config['year_level_map'][ $starting_year ] ) ? $config['year_level_map'][ $starting_year ] : null; | |
| if ( $level_id && ! pmpro_hasMembershipLevel( $level_id, $user_id ) ) { | |
| pmpro_changeMembershipLevel( $level_id, $user_id ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment