Created
May 5, 2023 05:54
-
-
Save ipokkel/d711ca6ecf29b40883ac6842bc9be604 to your computer and use it in GitHub Desktop.
Display the sponsored users of a sponsor with a shortcode for PMPro Sponosored Members. [pmpro_sponsored_children]
This file contains 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 | |
/** | |
* Add a shortcode to display a list of sponsored children. | |
* | |
* @param array $atts { | |
* Attributes for the shortcode. | |
* | |
* @type int $user_id The user ID of the sponsor. | |
* } | |
* | |
* @return string $html The HTML output for the shortcode. | |
* | |
* 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_sponsored_children_shortcode( $atts ) { | |
// Get shortcode attributes. | |
$atts = shortcode_atts( | |
array( | |
'user_id' => get_current_user_id(), | |
), | |
$atts, | |
'pmpro_sponsored_children' | |
); | |
// Get the user ID. | |
$user_id = intval( $atts['user_id'] ); | |
// bail if required function or user_id is not set. | |
if ( ! function_exists( 'pmprosm_getChildren' ) || empty( $user_id ) ) { | |
return ''; | |
} | |
$sponsored_ids = pmprosm_getChildren( $user_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 ) ) { | |
$html = '<strong>Sponsored Members</strong>'; | |
$html .= '<ul>'; | |
foreach ( $sponsored_members as $sponsored_member ) { | |
$html .= '<li>' . esc_html( $sponsored_member->data->display_name ) . '</li>'; | |
} | |
$html .= '</ul>'; | |
} | |
return $html; | |
} | |
add_shortcode( 'pmpro_sponsored_children', 'my_pmpro_sponsored_children_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment