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

Embed Snippet on Your Site

MemberPress: Display Current User’s Memberships Statuses And Renewal Dates

This code snippet adds the new [mp_membership_info] shortcode.

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

To display transaction status only in specific cases (e.g. only failed transactions), remove statuses you don’t want to be displayed on this line:

AND txn.status IN ('complete', 'failed', 'pending', 'refunded')",

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_custom_membership_info() {
    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', 'failed', 'pending', 'refunded')", // Remove any status if not needed
        $user_id
    ));
    if (empty($transactions)) {
        return 'No active memberships found.';
    }
    $output = '<div class="mp-membership-info">';
    foreach ($transactions as $txn) {
        // Format renewal date
        $renewal_date = $txn->expires_at ? date("F j, Y", strtotime($txn->expires_at)) : 'N/A';
        $output .= '<p>';
        $output .= '<strong>Membership:</strong> ' . esc_html($txn->post_title) . '<br>';
        $output .= '<strong>Status:</strong> ' . esc_html(ucfirst($txn->status)) . '<br>';
        $output .= '<strong>Renewal Date:</strong> ' . esc_html($renewal_date) . '<br>';
        $output .= '</p>';
    }
    $output .= '</div>';
    return $output;
}
add_shortcode('mp_membership_info', 'mp_custom_membership_info');

Comments

Add a Comment