Filter uncanny-automator-pro

uap_pmpro_membership_level_end_date

Filters the membership level end date for Paid Memberships Pro, allowing modification before it's finalized.

add_filter( 'uap_pmpro_membership_level_end_date', $callback, 10, 4 );

Description

Filters the end date for a Paid Memberships Pro membership level set by Uncanny Automator. Modify the expiration date before it's applied to the user's membership, allowing custom logic for expiration. The `$end_date` parameter is a string formatted as 'YYYY-MM-DD'.


Usage

add_filter( 'uap_pmpro_membership_level_end_date', 'your_function_name', 10, 4 );

Parameters

$end_date (mixed)
This parameter holds the calculated end date for the user's membership level, which can be modified by the filter.
$user_id (mixed)
This parameter holds the calculated end date for the user's membership level.
$pmpro_membership_level (mixed)
This parameter contains the ID of the user whose membership expiration date is being filtered.
$start_date (mixed)
This parameter holds the Paid Memberships Pro membership level object currently being processed, which contains details about the membership level's settings.

Return Value

The filtered value.


Examples

/**
 * Example function to modify the membership end date for Paid Memberships Pro via Uncanny Automator.
 * This function might be used to extend or shorten the membership duration based on certain conditions.
 *
 * @param string $end_date           The current calculated end date, formatted as a string (e.g., "'YYYY-MM-DD'").
 * @param int    $user_id            The ID of the user whose membership is being modified.
 * @param object $pmpro_membership_level The Paid Memberships Pro level object.
 * @param string $start_date         The membership start date, formatted as a string (e.g., "'YYYY-MM-DD'").
 *
 * @return string The modified end date, still formatted as a string (e.g., "'YYYY-MM-DD'").
 */
function my_automator_pmpro_modify_membership_end_date( $end_date, $user_id, $pmpro_membership_level, $start_date ) {
	// Retrieve the current end date from the input string, removing the surrounding single quotes.
	$current_end_date_string = trim( $end_date, "'" );
	$current_end_timestamp   = strtotime( $current_end_date_string );

	// Example: If the membership level is "Gold" and the user is a premium member, extend the end date by 7 days.
	if ( $pmpro_membership_level->name === 'Gold' ) {
		// Check if the user has an active membership for this level (you might need to add more robust checks here).
		// For this example, we'll assume if we're in this filter, it's an active membership context.
		$extended_end_timestamp = strtotime( '+7 days', $current_end_timestamp );
		$new_end_date_string    = date( 'Y-m-d', $extended_end_timestamp );

		// Reformat the date back into the expected string format with single quotes.
		return "'" . $new_end_date_string . "'";
	}

	// Example: If the membership level is "Basic" and the user is not in a specific group,
	// shorten the end date by 3 days.
	// This assumes you have a way to check user group membership, which is not directly provided by PMPRO alone.
	// For demonstration, let's use a hypothetical function `my_user_is_in_specific_group()`.
	/*
	if ( $pmpro_membership_level->name === 'Basic' && ! my_user_is_in_specific_group( $user_id ) ) {
		$shortened_end_timestamp = strtotime( '-3 days', $current_end_timestamp );
		$new_end_date_string     = date( 'Y-m-d', $shortened_end_timestamp );
		return "'" . $new_end_date_string . "'";
	}
	*/

	// If no modifications are made, return the original end date.
	return $end_date;
}
add_filter( 'uap_pmpro_membership_level_end_date', 'my_automator_pmpro_modify_membership_end_date', 10, 4 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/paid-memberships-pro/actions/pmp-addusertomembership.php:183

if ( ! empty( $set_expiration_date ) ) {
					$formatted_expiration_date = pmprosed_fixDate( $set_expiration_date );
					$end_timestamp = strtotime( $formatted_expiration_date );
				}
			}
			
			$end_date   = "'" . date_i18n( 'Y-m-d', $end_timestamp ) . "'";
			$end_date   = apply_filters( 'uap_pmpro_membership_level_end_date', $end_date, $user_id, $pmpro_membership_level, $start_date );

			$level = array(
				'user_id'         => $user_id,
				'membership_id'   => $pmpro_membership_level->id,
				'code_id'         => 0,
				'initial_payment' => 0,
				'billing_amount'  => 0,


Scroll to Top