Home / Archive / MemberPress: Display Current User’s Memberships Renewal Dates
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Display Current User’s Memberships Renewal Dates

This code snippet adds the new [mp_membership_renewal_date] shortcode.

Adding this shortcode anywhere on the website displays a list of active MemberPress memberships and their renewal dates for the current (logged-in) user.

To update the error message displayed when a logged-out user tries to access this data, adjust the "You need to be logged in to view this information" error text on this line:

return 'You need to be logged in to view this information.';

To update the error message displayed when the logged-in user has no active memberships, adjust the "No active memberships found" error text on this line:

return 'No active memberships found.';

Code Preview
php
<?php
function mp_membership_renewal_date() {
    if (!is_user_logged_in()) {
        return 'You need to be logged in to view this information.';
    }
    global $wpdb;
    $user_id = get_current_user_id();
    // Query to get all active MemberPress transactions for the current user
    $transactions = $wpdb->get_results($wpdb->prepare(
        "SELECT txn.*, p.post_title 
         FROM {$wpdb->prefix}mepr_transactions txn
         JOIN {$wpdb->prefix}posts p ON txn.product_id = p.ID
         WHERE txn.user_id = %d
         AND txn.status IN ('complete', 'confirmed')", // Add other necessary statuses here
        $user_id
    ));
    if (empty($transactions)) {
        return 'No active memberships found.';
    }
    // Prepare output for renewal date
    $renewal_output = '<div class="mp-membership-renewal-date">';
    foreach ($transactions as $txn) {
        $renewal_date = $txn->expires_at ? date("F j, Y", strtotime($txn->expires_at)) : 'N/A';
        $renewal_output .= '<p>';
        $renewal_output .= '<strong>Membership:</strong> ' . esc_html($txn->post_title) . '<br>';
        $renewal_output .= '<strong>Renewal Date:</strong> ' . esc_html($renewal_date) . '<br>';
        $renewal_output .= '</p>';
    }
    $renewal_output .= '</div>';
    return $renewal_output;
}
add_shortcode('mp_membership_renewal_date', 'mp_membership_renewal_date');

Comments

Add a Comment