Created
October 12, 2021 16:06
-
-
Save ipokkel/dee4b53a669bdf2a306e754ad56d7f37 to your computer and use it in GitHub Desktop.
Show a list of the sponsored member names on the parent's profile page on the Member Directory's Profile Page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Show a list of the sponsored (child) member accounts on the parent's profile page on the Member Directory's Profile page. | |
* | |
* Display sponsored member's full names of user has both first and last name meta, otherwise default to their display name. | |
* | |
* 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 show_sponsored_child_accounts_pmpro_member_profile_after( $pu ) { | |
// Let's only do this if PMPro Sponsored Members is active. | |
if ( ! function_exists( 'pmprosm_getChildren' ) ) { | |
return; | |
} | |
// Get sponsored member's user IDs | |
$sponsored_ids = pmprosm_getChildren( $pu->ID ); | |
if ( empty( $sponsored_ids ) ) { | |
return; | |
} | |
$sponsored_list = new WP_User_Query( | |
array( | |
'include' => $sponsored_ids, | |
'orderby' => 'name', | |
'order' => 'ASC', | |
), | |
); | |
$sponsored_members = $sponsored_list->get_results(); | |
if ( ! empty( $sponsored_members ) ) { | |
echo '<strong>Sponsored Members</strong>'; | |
echo '<ul>'; | |
foreach ( $sponsored_members as $sponsored_member ) { | |
$member_id = $sponsored_member->ID; | |
$first_name = get_user_meta( $member_id, 'first_name', true ); | |
$last_name = get_user_meta( $member_id, 'last_name', true ); | |
$name = $first_name && $last_name ? $first_name . ' ' . $last_name : $sponsored_member->data->display_name; | |
echo '<li>' . esc_html( $name ) . '</li>'; | |
} | |
echo '</ul>'; | |
} | |
} | |
add_action( 'pmpro_member_profile_after', 'show_sponsored_child_accounts_pmpro_member_profile_after' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To list sponsored member account users' display names instead of first and last names see https://gist.github.com/kimcoleman/38d4a32604002c5d88938a0d3f36233e