Home / Archive / MemberPress: Add Shortcode To Output the User’s Subscription Expiration Date
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Add Shortcode To Output the User’s Subscription Expiration Date

Adds the following shortcode: [mepr-sub-expiration membership=' ']. The shortcode will output the expiration date of the specific membership for the active user when used.

The shortcode should be used by specifying the membership ID within the shortcode. In the example shortcode, the dummy membership ID is 123, and the shortcode looks like this:

[mepr-sub-expiration membership='123']

Code Preview
php
<?php
function mepr_sub_expiration_shortcode( $atts = [], $content = null, $tag = '' ) { 
  $sub_expire_html = '';
  
  if( $atts['membership'] && is_numeric( $atts['membership'] ) ) {
    $date_str = MeprUser::get_user_product_expires_at_date(get_current_user_id(), $atts['membership']);
	
	if ( $date_str ) {
      $date = date_create( $date_str );
      $sub_expire_html = "<div>Expires: " . date_format( $date,"Y/m/d" ) . "</div>";
	}
  }
	
  return $sub_expire_html;
}
add_shortcode('mepr-sub-expiration', 'mepr_sub_expiration_shortcode');

Comments

Add a Comment