Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/e254a64efe38d2967cb014d0805a98ec to your computer and use it in GitHub Desktop.
Save kimwhite/e254a64efe38d2967cb014d0805a98ec 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 Level 10 membership has expired. Change Line 62 to your level ID
* Edit Line 65 with your custom message and link. Update CSS to your colors etc.
*
* 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, or it is not level ID 10.
if ( empty( $level ) || 'expired' !== $level->status || (int) $level->membership_id !== 10 ) {
return;
}
$message = 'Your membership has expired. <a href=' . esc_url( pmpro_url( 'checkout', '?level=' . $level->membership_id ) ) . '>Click here to renew your membership.</a>';
echo '<div class="pmpro_banner_renewal_wrapper"><h4>' . wp_kses_post( $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