Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JarrydLong/deb3d4250596303e85b254c0fdb6b87b to your computer and use it in GitHub Desktop.
Save JarrydLong/deb3d4250596303e85b254c0fdb6b87b to your computer and use it in GitHub Desktop.
Display a banner that notifies users about their upcoming expiration - Memberlite method.
<?php
/**
* This code will display a renewal reminder notification banner at the top of your website for members whose membership
* level will expire within 7 days of the date they visit your site.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* Note: When adding to your Customizations Plugin, be careful not to include the opening php tag on line 1 above.
*/
function memberlite_show_banner_renewal_message(){
global $pmpro_pages;
// Bail early if the current user does not have a membership level.
if ( ! pmpro_hasMembershipLevel() ) {
return;
}
// Load custom CSS for banner.
?>
<style>
.pmpro_banner_renewal_wrapper h4 {
color: white;
margin: 0;
padding: 1rem;
text-align: center;
}
.pmpro_banner_renewal_wrapper a {
color: white;
text-decoration: underline;
}
.pmpro_banner_renewal_wrapper a:hover {
color: rgba(255,255,255,0.8);
}
</style>
<?php
$user_id = get_current_user_id();
$levels = pmpro_getMembershipLevelsForUser( $user_id );
// Bail if this is the checkout page.
if ( is_page( $pmpro_pages['checkout'] ) ) {
return;
}
$enddates = array();
foreach( $levels as $level ) {
// Bail if the user does not have an enddate set.
if ( ! empty( $level->enddate ) ) {
$expiring[$level->enddate] = $level;
}
}
//Order by level that is expiring soonest
ksort( $expiring );
$message = "";
$today = time();
$expiring_level = reset( $expiring );
// if today is more than 7 days before enddate, bail.
if ( $today <= strtotime( '- 7 days', $expiring_level->enddate ) ) {
return;
}
$message = 'Your ' . $expiring_level->name . ' membership will expire soon. <a href="' . pmpro_url( "checkout", "?level=" . $expiring_level->id ) . '"> Click here to renew membership.</a>';
echo '<div class="pmpro_banner_renewal_wrapper banner banner_secondary"><h4> ' . $message . ' </h4></div>';
}
add_action( 'before_page', 'memberlite_show_banner_renewal_message' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment