How to display only the $expire_date on a page?

I have a need to display just the expiration date for a logged-in user with a membership. If there are more than one membership, the latest date should be shown.

  • Nastia
    • Support Rock Star

    Hello @Dick, I hope you are well today!

    On the user account page that membership is showing the expire date, like it shows in the attached screenshot. Is this what you are looking for?

    Would you please clarify for me where you wish to display the information, on the account page or somewhere else?

    Please advise,

    Cheers,
    Nastia

  • Dick
    • Design Lord, Child of Thor

    Thanks for the quick reply.

    I am creating a 'membership ID card' that the user can print (nothing to do with the account page). I know how to get most of the user properties but am stuck with how to get the expire date since it is on the relationship between the user and the membership.

  • Sajid
    • DEV MAN’s Sidekick

    Hi @Dick,

    Hope you are doing good today :slight_smile:

    To get membership expiry date of a specific member, you first have a subscription object of a member. Using the API of membership it can be done quite easily like below.

    $api = ms_api();
    $subscription = $api->get_subscription($user_id, $membership);

    In this example we first call the M2 API and then with get_subscription method you can get all data associated of a specific membership of a specific member. First parameter is integer ID of a user and the second parameter can be membership ID, name or slug.

    It will return subscription model, including methods, constants and varibles and later you can display expiry date and also format the way M2 does by using following code.

    MS_Helper_Period::format_date( $subscription->expire_date );

    I have come up with following shortcode (for someone else not code savy) to display the expiry date of a membership associated to a user (both arguments are required).

    Add following code in functions.php file of your theme or child theme or even use it as mu-plugin.

    function wpmu_display_membership_expiry_date($atts){
    
    	$returner = '';
    
    	$args = shortcode_atts( array('user_id' => '',
    							'membership' => ''
    							), $atts);
    
    	extract($args);
    
    	// only run if M2 is active
    	if ( defined( 'MS_PLUGIN' ) ){
    	 	$api = ms_api();
    		$subscription = $api->get_subscription($user_id, $membership);
    		if($subscription){
    
    			if( $subscription->expire_date )
    				$returner = MS_Helper_Period::format_date( $subscription->expire_date );
    			else
    				$returner = 'Never';
    		}else{
    			$returner = 'specify correct user id or membership id/slug/name';
    		}
    
    		return $returner;
    	}
    
    }
    add_shortcode('show_ms_expiry_date','wpmu_display_membership_expiry_date', 10);

    Now, use this shortcode to display the expiry date. Don't forget to change the user ID and membership name accordingly.

    [show_ms_expiry_date user_id="39" membership="Test Membership Update"]

    Take care and have a nice day :slight_smile:

    Kind Regards,
    Sajid J

  • Sajid
    • DEV MAN’s Sidekick

    Hi @Dick,

    Hope you are doing good today :slight_smile:

    If its for current logged in users then just replace the user_id variable with current user ID. You can get current user ID easy as explained in this article on codex.
    https://codex.wordpress.org/Function_Reference/get_current_user_id

    Hope that helps! If you still need assistance, don't hesitate to ask.

    Take care and have a nice day :slight_smile:

    Kind Regards,
    Sajid J

  • Sajid
    • DEV MAN’s Sidekick

    Hi @Dick,

    Hope you are doing good today :slight_smile:

    To get the ID(s) of membership of a user you first need to get current get_current_member() model from $api object and then, from current member's object you can get IDs with get_membership_ids().

    Our membership 2 plugin have really very well written API documentation. You can find everything you want in Membership 2 > Help > API Docs.

    Hope that helps! If you still need assistance, don't hesitate to ask.

    Take care and have a nice day :slight_smile:

    Kind Regards,
    Sajid J

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.