Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/5a2f8ed17c59a7cf363c42e5b3c83b4f to your computer and use it in GitHub Desktop.
Save ipokkel/5a2f8ed17c59a7cf363c42e5b3c83b4f to your computer and use it in GitHub Desktop.
Add custom field for pmpro_member shortcode that display the user's level(s) and their expiration date(s)
<?php
/**
* This recipe adds the custom field my_memberships to the [pmpro_member] shortcode
* that will display a user's membership level(s) and expiration date(s).
*
* Supports Multiple Memberships Per User (MMPU) Add On to display all the user's membership levels.
*
* Example Usage of [pmpro_member field="my_memberships"]
* Membership Level: Starter
* Membership Expires: Never
*
* Membership Level: One Month
* Membership Expires: December 24, 2023
*
* Membership Level: Monthly
* Membership Expires: Recurring Until Cancelled
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_member_shortcode_custom_field_1669295230000( $r, $user_id, $field ) {
// Check if the field is the one we want to modify.
if ( 'my_memberships' !== $field ) {
return $r;
}
// Get all the user's membership levels and display the level name and expiration date for each level they have.
$levels = pmpro_getMembershipLevelsForUser( $user_id );
if ( ! empty( $levels ) ) {
$r = '';
foreach ( $levels as $level ) {
if ( ! empty( $level->enddate ) ) {
$enddate = date_i18n( get_option( 'date_format' ), $level->enddate );
} else {
if ( pmpro_isLevelFree( $level ) ) {
$enddate = 'Never';
} else {
$enddate = 'Recurring Until Cancelled';
}
}
$r .= 'Membership Level: ' . $level->name . '<br />Membership Expires: ' . $enddate . '<br /><br />';
}
}
return $r;
}
add_filter( 'pmpro_member_shortcode_field', 'my_pmpro_member_shortcode_custom_field_1669295230000', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment