Filter uncanny-automator-pro

uap_pmpro_membership_level_start_date

Filters the membership level start date, allowing customization before it's saved for a specific user and membership level.

add_filter( 'uap_pmpro_membership_level_start_date', $callback, 10, 2 );

Description

Filters the membership level start date for Paid Memberships Pro. Developers can modify the calculated start date before it's used to set a user's membership. The current time is provided as the default start date.


Usage

add_filter( 'uap_pmpro_membership_level_start_date', 'your_function_name', 10, 2 );

Parameters

$user_id (mixed)
This parameter provides the initial calculated start date for the membership, which is typically the current time in MySQL format.
$pmpro_membership_level (mixed)
This parameter holds the ID of the user for whom the membership level is being set.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the start date for a Paid Memberships Pro membership level.
 * This might be used to set a custom start date based on certain conditions,
 * for instance, to align with a promotional period or a specific user attribute.
 *
 * @param string $start_date The original start date string (e.g., "'2023-10-27 10:00:00'").
 * @param int    $user_id The ID of the user being added to the membership.
 * @param object $pmpro_membership_level The Paid Memberships Pro membership level object.
 * @return string The modified start date string, or the original if no changes are made.
 */
add_filter( 'uap_pmpro_membership_level_start_date', function( $start_date, $user_id, $pmpro_membership_level ) {

	// Example: If the membership level ID is 10 and the user is a subscriber,
	// set the start date to the beginning of the next month.
	if ( $pmpro_membership_level->id == 10 && user_can( $user_id, 'subscriber' ) ) {

		// Get the current date and time
		$now = current_time( 'timestamp' );

		// Calculate the first day of the next month
		$next_month_timestamp = strtotime( '+1 month', $now );
		$start_of_next_month = date( 'Y-m-01 00:00:00', $next_month_timestamp );

		// Format the date for the database
		$modified_start_date = "'" . esc_sql( $start_of_next_month ) . "'";

		return $modified_start_date;
	}

	// If no specific conditions are met, return the original start date.
	return $start_date;

}, 10, 3 );

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:170

return;
		}

		$new_level = null;
		if ( ! empty( $pmpro_membership_level->expiration_number ) ) {

			$start_date = apply_filters( 'uap_pmpro_membership_level_start_date', "'" . current_time( 'mysql' ) . "'", $user_id, $pmpro_membership_level );
			$end_timestamp = strtotime( '+ ' . $pmpro_membership_level->expiration_number . ' ' . $pmpro_membership_level->expiration_period, current_time( 'timestamp' ) );
			
			// does this level have a set expiration date?
			if ( function_exists( 'pmpro_getSetExpirationDate' ) ) {
				$set_expiration_date = pmpro_getSetExpirationDate( $pmpro_membership_level->id );
				if ( ! empty( $set_expiration_date ) ) {
					$formatted_expiration_date = pmprosed_fixDate( $set_expiration_date );


Scroll to Top