List memberships and linked pages?

Is there a shortcode that will allow me to show a user, on a single page, their subscriptions, and the pages associated with each subscription?

E.G.

DESIGN WORKING GROUP
* Font selection
* Colour palettes

PROJECTS
* Acme Company
* XYZ Inc.

  • Ash

    Hello @Mark Blevis

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    You can show the membership of an user with the following code:
    `function get_membership_of_user( $user_id = 0 ) {
    $user_id = $user_id == 0 ? get_current_user_id() : $user_id;
    $all_subscriptions = MS_Model_Relationship::get_subscriptions(
    array(
    'user_id' => $user_id,
    'status' => 'all',
    'meta_key' => 'expire_date',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    )
    );

    $memberships = array();
    foreach ( $all_subscriptions as $subscription ){
    $membership = $subscription->get_membership();
    $memberships[] = $membership->get_name_tag();
    }

    return implode( ' ', $memberships );
    }
    Now if you need membership of an user whose ID is 9, use like this:
    echo get_membership_of_user( 9 );
    If you want membership of current logged in user:
    echo get_membership_of_user();

    But this is not possible to show the associated pages with each subscription, it will be more complex and will need lots of custom code. Also, if you need to customise this then you can post a job in our job board where you can hire a developer to assist further: https://premium.wpmudev.org/wpmu-jobs/ Please note that, no WPMU official staff is available in the job board.

    Cheers
    Ash

  • Ash

    Hi Mark

    Sure :slight_smile: Try the following code:

    function get_membership_of_user( $atts ) {
    
    	$a = shortcode_atts( array(
    		'user_id' => get_current_user_id()
    	), $atts );
    
    	extract( $a );
    
    	$all_subscriptions = MS_Model_Relationship::get_subscriptions(
    		array(
    		'user_id' => $user_id,
    		'status' => 'all',
    		'meta_key' => 'expire_date',
    		'orderby' => 'meta_value',
    		'order' => 'DESC',
    		)
    	);
    
    	$memberships = array();
    	foreach ( $all_subscriptions as $subscription ){
    		$membership = $subscription->get_membership();
    		$memberships[] = $membership->get_name_tag();
    	}
    
    	return implode( ' ', $memberships );
    }
    
    add_shortcode( 'show_membership', 'get_membership_of_user' );

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Now to show memberships of logged in user:

    [show_membership]

    To show memberships of a specific user:

    [show_membership user_id="9"]

    Where 9 is the user ID.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Mark Blevis

    I added the file in content/mu-plugins/wpmudev-functions-m2p-showmembership with the following contents:

    <?php
    /**
     * WPMU dev Membership 2 Pro plugin
     * Shortcode code to display list memberships and linked pages
     * https://premium.wpmudev.org/forums/topic/list-memberships-and-linked-pages#post-955843
     */
    
    function get_membership_of_user( $atts ) {
    
    	$a = shortcode_atts( array(
    		'user_id' => get_current_user_id()
    	), $atts );
    
    	extract( $a );
    
    	$all_subscriptions = MS_Model_Relationship::get_subscriptions(
    		array(
    		'user_id' => $user_id,
    		'status' => 'all',
    		'meta_key' => 'expire_date',
    		'orderby' => 'meta_value',
    		'order' => 'DESC',
    		)
    	);
    
    	$memberships = array();
    	foreach ( $all_subscriptions as $subscription ){
    		$membership = $subscription->get_membership();
    		$memberships[] = $membership->get_name_tag();
    	}
    
    	return implode( ' ', $memberships );
    }
    
    add_shortcode( 'show_membership', 'get_membership_of_user' );

    When I add the shortcode [show_membership] to a page, it displays "[show_membership]" on the webpage.

    What am I doing wrong?

    Thanks!
    Mark

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.