Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JarrydLong/3a31883885fbfd147c656ec97a42753b to your computer and use it in GitHub Desktop.

Select an option

Save JarrydLong/3a31883885fbfd147c656ec97a42753b to your computer and use it in GitHub Desktop.
<?php // do not copy
/**
* Set membership expiration based on membership year.
* Membership year: Sept 1 – Aug 31
* Renewal window: Jul 1 – Aug 31
*
* 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/
*/
function tws_pmpro_membership_year_expiration($level) {
$now = current_time('timestamp');
$month = (int) date('n', $now);
$year = (int) date('Y', $now);
// July/Aug payments apply to next membership year
if ($month == 7 || $month == 8) {
$expire_year = $year + 1;
} else {
$expire_year = $year;
}
// Membership ends Aug 31
$expire_ts = strtotime($expire_year . '-08-31 23:59:59');
// Days remaining
$days_left = ceil(($expire_ts - $now) / DAY_IN_SECONDS);
if ($days_left < 1) {
$days_left = 1;
}
$level->expiration_number = $days_left - 1;
$level->expiration_period = 'Day';
return $level;
}
add_filter('pmpro_checkout_level', 'tws_pmpro_membership_year_expiration', 10, 1);
function tws_pmpro_membership_expiration_text($text, $level) {
$now = current_time('timestamp');
$month = (int) date('n', $now);
$year = (int) date('Y', $now);
if ($month == 7 || $month == 8) {
$expire_year = $year + 1;
} else {
$expire_year = $year;
}
$expiry = date_i18n('F j, Y', strtotime($expire_year . '-08-31'));
return 'Membership expires on ' . esc_html($expiry) . '.';
}
add_filter('pmpro_level_expiration_text', 'tws_pmpro_membership_expiration_text', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment