Home / Archive / MemberPress: Unsubscribed Memberships Shortcode
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Unsubscribed Memberships Shortcode

This code snippet adds [mepr-unsubscribed-memberships] shortcode to display an unordered list (Titles/links) of the logged-in user’s inactive/unsubscribed memberships.

The shortcode can be added to any page/post on the website.

Code Preview
php
<?php
add_shortcode( 'mepr-unsubscribed-memberships', function () {
	$args = array(
		'post_type' => 'memberpressproduct',
		'post_status' => 'publish',
		'posts_per_page' => -1,
	);
	
	$memberships = new WP_Query( $args );
	
	ob_start();
	
	if ( $memberships->have_posts() ) {
		echo '<ul>';
		while ( $memberships->have_posts() ) {
			$memberships->the_post();
			$post_ID = get_the_ID();
			// Check if the user has an active subscription to the current Membership
			if ( current_user_can( 'mepr-active', "membership:{$post_ID}" ) ) {
				continue;
			}
			// If not, print an li item with membership title and link
			echo '<li><a href="' . esc_url( get_the_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></li>';
		}
		echo '</ul>';
	}
	
	$output = ob_get_clean();
	
	return $output;
	
} );

Comments

Add a Comment