Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active March 23, 2021 08:02
Show Gist options
  • Save andrewlimaza/08d6e9d8dd647d2168774c8be46e6940 to your computer and use it in GitHub Desktop.
Save andrewlimaza/08d6e9d8dd647d2168774c8be46e6940 to your computer and use it in GitHub Desktop.
Custom shortcode to show member expiration dates or recurring.
<?php
/**
* Show the user's expiration date, or their next payment date.
* Show the user's previously expired date if they were cancelled, expired or cancelled by an admin.
* If none of thhe above is found, the string 'nothing found' will be returned.
*/
function my_show_user_enddate(){
if ( is_user_logged_in() && function_exists( 'pmpro_hasMembershipLevel' ) && pmpro_hasMembershipLevel() ){
global $current_user;
$current_user->membership_level = pmpro_getMembershipLevelForUser( $current_user->ID );
$user_enddate = $current_user->membership_level->enddate;
if( !$user_enddate == 0){
return 'Membership end date: ' . date_i18n('d-m-Y', $user_enddate);
}else{
// See if membership is recurring.
$recurring = pmpro_next_payment( $current_user->ID );
if ( $recurring) {
return date_i18n( 'd-m-Y', $recurring );
} else {
return 'Membership never expires.';
}
}
} elseif ( ! pmpro_hasMembershipLevel() ) {
//try to see if they had a previous membership level.
global $wpdb, $current_user;
$sql = "SELECT enddate FROM $wpdb->pmpro_memberships_users WHERE `user_id` = $current_user->ID AND `status` in ('cancelled', 'expired', 'admin_cancelled') ORDER BY enddate ASC LIMIT 1";
$results = $wpdb->get_results( $sql );
if ( ! empty( $results[0] ) ) {
$enddate = explode( " ", $results[0]->enddate );
return $enddate[0]; //return the date only, not time of expiration.
}else{
return 'nothing found';
}
}
}
add_shortcode( 'show_enddate', 'my_show_user_enddate' );
@kimwhite
Copy link

Changed line 21 to print date (ok. david did this for me)
$recurring = date( 'F j, Y', pmpro_next_payment( $current_user->ID ) );

Could use shortcode in your description.[show_enddate]

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