Subscription Info Shortcode

This shortcode can be used to display information about the current/any users subscription.
Parameters:

  • id – If not set, will default to the currently logged in user
  • subscription_plan – this can be used to target a particular subscription in case the user is subscribed to multiple plans
  • key – value that you want to retrieve. Possible keys: start_date, expiration_date, status, plan_name, plan_price, plan_duration

Usage:
[pms-subscription-info key="plan_name"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
add_shortcode( 'pms-subscription-info', 'pmsc_subscription_info_shortcode' );
function pmsc_subscription_info_shortcode( $atts ){
	$atts = shortcode_atts(	array(
			'id'                => get_current_user_id(),
			'subscription_plan' => '',
			'key'               => '',
		), $atts );
 
	if (empty($atts['key']) || empty($atts['id']))
		return;
 
	$args = array( 'user_id' => $atts['id'] );
 
	if( !empty( $atts['subscription_plan'] ) )
		$args['subscription_plan_id'] = $atts['subscription_plan'];
 
	$member = pms_get_member_subscriptions( $args );
 
	if ( empty( $member ) )
		return '';
 
 	$subscription_plan = pms_get_subscription_plan( $member[0]->subscription_plan_id );
 
	$subscription_statuses = pms_get_member_subscription_statuses();
 
	switch ($atts['key']) {
		case 'start_date':
			return ucfirst( date_i18n( 'j F, Y H:i', strtotime( $member[0]->start_date ) ) );
			break;
		case 'expiration_date':
			return ucfirst( date_i18n( 'j F, Y H:i', strtotime( $member[0]->expiration_date ) ) );
			break;
		case 'next_payment':
			return ucfirst( date_i18n( 'j F, Y H:i', strtotime( $member[0]->billing_next_payment ) ) );
			break;
		case 'status':
			return $subscription_statuses[$member[0]->status];
			break;
		case 'plan_name':
			return $subscription_plan->name;
			break;
		case 'plan_price':
			return $subscription_plan->price;
			break;
		case 'plan_duration':
			return $subscription_plan->duration . ' ' . $subscription_plan->duration_unit;
			break;
		case 'default':
			return;
			break;
	}
}