Skip to content

Instantly share code, notes, and snippets.

@escopecz
Last active April 3, 2019 04:39
Show Gist options
  • Save escopecz/1a6814357116f215f5c8 to your computer and use it in GitHub Desktop.
Save escopecz/1a6814357116f215f5c8 to your computer and use it in GitHub Desktop.
Set expiration date for Easy Digital Downloads Wordpress plugin

Here is solution for Easy Digital Downloads WP plugin to disable downloads one year after purchase.

Place these 3 functions to /wp-content/themes/{your_theme}/functions.php:

/**
 * Function returns expiration date based on post / payment date.
 * Replace '365 day' with your time period.
 * 
 * @param string date of post / payment
 * @return string date in unix timestamp format
 */
function ow_edd_get_expiration_date($post_date) {

	// Create expire date 365 days from payment date
	$expiration_date = date("U", strtotime($post_date . " + 365 day"));

	return $expiration_date;
}

/**
 * Hook from before the package is downloaded.
 * It modify expire date which then allow or deny
 * the user to download the package.
 * 
 * @param array request arguments
 * @return array request arguments
 */
function ow_edd_license_length( $args ) {

	// Do not continue when payment key is not filled or empty
	if (!isset($args['key']) || !$args['key']) {
		return $args;
	}

	global $wpdb;

	// Get post / payment date by payment key from request.
	$post_date = $wpdb->get_var( 'SELECT p.post_date 
								FROM wp_postmeta pm
								LEFT JOIN wp_posts p ON p.ID = pm.post_id
								WHERE pm.meta_key = "_edd_payment_purchase_key"
								AND pm.meta_value = "'.addslashes($args['key']).'"');

	// Do not continue if date was not find in the database
	if (!$post_date) {
		return $args;
	}

	// Set expire date
	$args['expire'] = ow_edd_get_expiration_date($post_date);

	// Return args with modified expire date
	return $args;

}
add_filter( 'edd_process_download_args', 'ow_edd_license_length');

/**
 * Hook is fired on payment detail page and
 * adds new table row - Expiration Date
 * 
 * @param Object payment post info
 * @return void
 */
function ow_edd_expiration_date_template($payment, $edd_receipt_args) {

  $expiration_date = ow_edd_get_expiration_date($payment->post_date);
  $date_fromat  = get_option('date_format');
  $link = '';

  if (time() > $expiration_date) {
    $link = ' <br /><a href="/pricing-signup/" class="btn btn-info btn-xl">Click here to renew your membership</a>';
  }

  echo '<tr>';
  echo '<td><strong>Expiration Date</strong></td>';
  echo '<td>'.date($date_fromat, $expiration_date).$link.'</td>';
  echo '</tr>';

}
add_action('edd_payment_receipt_after','ow_edd_expiration_date_template');
@ankitsuryawanshi
Copy link

ankitsuryawanshi commented Apr 3, 2019

Did try adding it, but it doesn't work. Can you please update the code?
Also, after adding the code, on the purchase confirmation page, which displays the download option for the product, it vanishes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment