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

Embed Snippet on Your Site

MemberPress: Display Current User’s Memberships Statuses

This code snippet adds the new [mp_membership_status] shortcode.

Adding this shortcode anywhere on the website displays a list of active MemberPress memberships and their statuses 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_membership_status() {
    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 status
    $status_output = '<div class="mp-membership-status">';
    foreach ($transactions as $txn) {
        $status_output .= '<p>';
        $status_output .= '<strong>Membership:</strong> ' . esc_html($txn->post_title) . '<br>';
        $status_output .= '<strong>Status:</strong> ' . esc_html(ucfirst($txn->status)) . '<br>';
        $status_output .= '</p>';
    }
    $status_output .= '</div>';
    return $status_output;
}
add_shortcode('mp_membership_status', 'mp_membership_status');

Comments

Add a Comment