Forked from greathmaster/pmpro-sponsored-members-assign-to-parent.php
Last active
June 4, 2024 14:19
-
-
Save kimcoleman/76aa6bd9da2506ba53beaa41d6ba497e to your computer and use it in GitHub Desktop.
Retroactively assign a user to a Parent account when using the Sponsored Members Add On.
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
/** | |
* Retroactively assign a user to a Parent account when using the Sponsored Members Add On. | |
* Enter the user ID of parent account in the "Assign to Parent Account" on the "Edit Profile" screen. | |
* | |
* Note: The user's active Membership Level must be the correct "Child" level for that Parent/Child relationship. | |
* | |
*/ | |
function pmprosm_assign_child_members( $profileuser ) { | |
if ( function_exists( 'pmprosm_getSponsor' ) && current_user_can( 'edit_users' ) ) { | |
// Get the parent for this user. | |
$parent = pmprosm_getSponsor( $profileuser->ID ); | |
if ( ! empty( $parent ) ) { | |
$parent_id = $parent->ID; | |
} else { | |
$parent_id = ''; | |
} | |
// Show the field to update the parent for this user. | |
if ( empty( $parent_id ) ) { ?> | |
<table class="form-table"> | |
<tr> | |
<th> | |
<label for="assign_child_member"><?php _e('Assign to Parent Account'); ?></label> | |
</th> | |
<td> | |
<input type="text" value="<?php echo $parent_id; ?>" name="assign_child_member" id="assign_child_member" /> | |
<p class="description">Enter the Parent Account's user ID for this user.</p> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
} | |
} | |
add_action( 'show_user_profile', 'pmprosm_assign_child_members', 20, 1 ); | |
add_action( 'edit_user_profile', 'pmprosm_assign_child_members', 20, 1 ); | |
function pmprosm_profile_assign_child_members( $user_id, $old_user_data ) { | |
if ( isset( $_POST[ 'assign_child_member' ] ) && (int)( $_POST[ 'assign_child_member' ] ) ) { | |
// Get the vars. | |
$parent_id = $_POST[ 'assign_child_member' ]; | |
$sponsored_code = pmprosm_getCodeByUserID( $parent_id ); | |
$membership_level_id = (int)$_POST[ 'membership_level' ]; | |
// Check that the user has a child level. | |
if ( pmprosm_isSponsoredLevel( $membership_level_id ) ) { | |
// Set their new membership level and assign their sponsor. | |
pmprosm_changeMembershipLevelWithCode( $membership_level_id, $user_id, $sponsored_code ); | |
pmprosm_addDiscountCodeUse( $user_id, $membership_level_id, $sponsored_code ); | |
} | |
} | |
} | |
add_action( 'profile_update', 'pmprosm_profile_assign_child_members', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Assign a “Parent” Account to an Existing User or Member: Sponsored Members Add On Recipe" at Paid Memberships Pro here: https://www.paidmembershipspro.com/assign-parent-to-user-sponsored-members-add-on-recipe/