Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/79e9e91e98e42119a43fa0a6c132fe0e to your computer and use it in GitHub Desktop.
Save ipokkel/79e9e91e98e42119a43fa0a6c132fe0e to your computer and use it in GitHub Desktop.
Add the `all_membership_names` field for the `[pmpro_member]` shortcode to display a comma separated list of all the active membership level names for the current user.
<?php
/**
* This recipe adds the custom field all_memberships_names for the [pmpro_member] shortcode
* that will display a comma separated list of all the user's membership level(s).
*
* Example Usage of [pmpro_member field="all_memberships_names"]
*
* 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_all_memberships_names( $r, $user_id, $field ) {
// Check if the field is the one we want to modify.
if ( 'all_memberships_names' !== $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 );
$level_names = wp_list_pluck( $levels, 'name' );
if ( ! empty( $levels ) && ! empty( $level_names ) ) {
// Return the level names as a comma separated string.
$r = implode( ', ', $level_names );
} else {
$r = '';
}
return $r;
}
add_filter( 'pmpro_member_shortcode_field', 'my_pmpro_member_shortcode_custom_field_all_memberships_names', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment