Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Forked from kimwhite/my-show-user-enddate.php
Last active November 16, 2020 06:03
Show Gist options
  • Save ipokkel/049c1a056ef935262a7fd6ac8d86d619 to your computer and use it in GitHub Desktop.
Save ipokkel/049c1a056ef935262a7fd6ac8d86d619 to your computer and use it in GitHub Desktop.
Custom shortcode to show member expiration dates or recurring. [show_enddate]
<?php
/**
* Show the user's expiration date, or their next payment date.
* [show_enddate] ( show end date, show enddate, show_endate )
* 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.
*
* 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 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( 'd-m-Y', $user_enddate );
} else {
// See if membership is recurring.
$recurring = date( 'F j, Y', pmpro_next_payment( $current_user->ID ) );
if ( $recurring ) {
return $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 );
$user_enddate = strtotime( $enddate[0] );
return date( 'F j, Y', $user_enddate );
} else {
return 'nothing found';
}
}
}
add_shortcode( 'show_enddate', 'my_show_user_enddate' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment