Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active October 31, 2023 06:40
Show Gist options
  • Save strangerstudios/ff354cd84254b856ef5194c4259c9e38 to your computer and use it in GitHub Desktop.
Save strangerstudios/ff354cd84254b856ef5194c4259c9e38 to your computer and use it in GitHub Desktop.
Shortcode to show a member's expiration date with Paid Memberships Pro
/*
Shortcode to show a member's expiration date.
Add this code to your active theme's functions.php or a custom plugin.
Then add the shortcode [pmpro_expiration_date] where you want the current user's
expiration date to appear.
If the user is logged out or doesn't have an expiration date, then --- is shown.
*/
function pmpro_expiration_date_shortcode( $atts ) {
//make sure PMPro is active
if(!function_exists('pmpro_getMembershipLevelForUser'))
return;
//get attributes
$a = shortcode_atts( array(
'user' => '',
), $atts );
//find user
if(!empty($a['user']) && is_numeric($a['user'])) {
$user_id = $a['user'];
} elseif(!empty($a['user']) && strpos($a['user'], '@') !== false) {
$user = get_user_by('email', $a['user']);
$user_id = $user->ID;
} elseif(!empty($a['user'])) {
$user = get_user_by('login', $a['user']);
$user_id = $user->ID;
} else {
$user_id = false;
}
//no user ID? bail
if(!isset($user_id))
return;
//get the user's level
$level = pmpro_getMembershipLevelForUser($user_id);
if(!empty($level) && !empty($level->enddate))
$content = date(get_option('date_format'), $level->enddate);
else
$content = "---";
return $content;
}
add_shortcode('pmpro_expiration_date', 'pmpro_expiration_date_shortcode');
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Show a Member’s Expiration Date in a Page, Post, Widget, etc. via Shortcode" at Paid Memberships Pro here: https://www.paidmembershipspro.com/show-members-expiration-date-page-post-widget-etc-via-shortcode/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment