Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created April 19, 2022 08:30
Show Gist options
  • Save ipokkel/84aaacd310f50aab7d66a6a3cee7c665 to your computer and use it in GitHub Desktop.
Save ipokkel/84aaacd310f50aab7d66a6a3cee7c665 to your computer and use it in GitHub Desktop.
Display a notification banner for members with an expired membership. #paid-memberships-pr #pmpro
<?php
/**
* Display a renewal reminder notification banner at the top of your website
* for members whose membership have expired.
*
* 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 pmpro_show_banner_expired_message() {
// Bail early if the current user has a membership level.
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) || pmpro_hasMembershipLevel() ) {
return;
}
global $pmpro_pages;
// Bail if this is the checkout page.
if ( is_page( $pmpro_pages['checkout'] ) ) {
return;
}
// Load custom CSS for banner.
?>
<style>
.pmpro_banner_renewal_wrapper {
background-color: salmon;
}
.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();
// Check for an expired level.
global $wpdb;
$level = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->pmpro_memberships_users} WHERE user_id = %d AND status = 'expired' ORDER BY id DESC LIMIT 1", $user_id ) );
// Bail if there is no expired level
if ( empty( $level ) || 'expired' !== $level->status ) {
return;
}
$message = 'Your membership has expired. <a href=' . pmpro_url( 'checkout', '?level=' . $level->membership_id ) . '> Click here to renew your membership.</a>';
echo '<div class="pmpro_banner_renewal_wrapper"><h4> ' . $message . ' </h4></div>';
}
add_action( 'wp_head', 'pmpro_show_banner_expired_message' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment