Halloween Sale 15% off Ends in See the plans
PHP

MemberPress: List the Member's Active Memberships

Adds a [wpide_mepr_memberships] shortcode that lists the logged-in member's active memberships by name, for welcome boxes and account pages.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! class_exists( 'MeprUser' ) ) {
    return;
}

add_shortcode( 'wpide_mepr_memberships', function ( $atts ) {
    $atts = shortcode_atts( array( 'none' => 'You do not have an active membership.' ), $atts, 'wpide_mepr_memberships' );

    if ( ! is_user_logged_in() ) {
        return '';
    }

    $member = new MeprUser( get_current_user_id() );
    $ids    = array_unique( array_map( 'intval', (array) $member->active_product_subscriptions( 'ids' ) ) );

    if ( ! $ids ) {
        return '<p class="wpide-mepr-none">' . esc_html( $atts['none'] ) . '</p>';
    }

    $html = '<ul class="wpide-mepr-memberships">';
    foreach ( $ids as $id ) {
        $html .= '<li>' . esc_html( get_the_title( $id ) ) . '</li>';
    }

    return $html . '</ul>';
} );

Notes

Shows the optional none="..." text when the member has no active membership.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.