Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Last active March 9, 2021 20:38
Show Gist options
  • Save kimcoleman/d24c430971dade95a130c8bf74ba2bed to your computer and use it in GitHub Desktop.
Save kimcoleman/d24c430971dade95a130c8bf74ba2bed to your computer and use it in GitHub Desktop.
Show the parent account's expiration for the child.
<?php
/*
* Show the parent account's expiration for the child in various places on frontend and admin pages in PMPro.
*
* 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 replace_parent_expiration_for_child( $user_id ) {
// Return if functions are not available.
if ( ! function_exists( 'pmprosm_getSponsor' ) ) {
return;
}
$sponsor = pmprosm_getSponsor( $user_id );
if ( ! empty( $sponsor ) ) {
$sponsor_level = pmpro_getMembershipLevelForUser( $sponsor->ID );
$enddate = $sponsor_level->enddate;
return $enddate;
}
return;
}
/**
* Replaces the expiration text shown on the Membership Account page for the child account.
*
*/
function use_parent_expiration_date_pmpro_account_membership_expiration_text( $expiration_text, $level ) {
global $current_user;
if ( function_exists( 'replace_parent_expiration_for_child' ) ) {
$new_expiration_text = replace_parent_expiration_for_child( $current_user->ID );
if ( ! empty( $new_expiration_text ) ) {
$expiration_text = date_i18n( get_option( 'date_format' ), $new_expiration_text );
}
}
return $expiration_text;
}
add_filter( 'pmpro_account_membership_expiration_text', 'use_parent_expiration_date_pmpro_account_membership_expiration_text', 10 , 2 );
/**
* Replaces the expiration text shown on other areas, including places like the Membership Card or Members Directory.
*
*/
function use_parent_expiration_date_pmpro_get_membership_level_for_user( $all_membership_levels, $user_id ) {
if ( ! function_exists( 'replace_parent_expiration_for_child' ) ) {
return $all_membership_levels;
}
$new_expiration_text = replace_parent_expiration_for_child( $user_id );
if ( ! empty( $new_expiration_text ) ) {
$all_membership_levels->enddate = $new_expiration_text;
}
return $all_membership_levels;
}
add_filter( 'pmpro_get_membership_level_for_user', 'use_parent_expiration_date_pmpro_get_membership_level_for_user', 10, 2 );
/**
* Replaces the expiration text shown on the Members list in the WordPress admin.
*
*/
function use_parent_expiration_date_pmpro_memberslist_expires_column( $expiration_text, $user_object ) {
if ( function_exists( 'replace_parent_expiration_for_child' ) ) {
$new_expiration_text = replace_parent_expiration_for_child( $user_object->ID );
if ( ! empty( $new_expiration_text ) ) {
$expiration_text = date_i18n( get_option( 'date_format' ), $new_expiration_text );
}
}
return $expiration_text;
}
add_filter( 'pmpro_memberslist_expires_column', 'use_parent_expiration_date_pmpro_memberslist_expires_column', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment