Skip to content

Instantly share code, notes, and snippets.

@tnorthcutt
Last active December 27, 2017 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnorthcutt/57dd2c07adf4ec651f02385dc41c1938 to your computer and use it in GitHub Desktop.
Save tnorthcutt/57dd2c07adf4ec651f02385dc41c1938 to your computer and use it in GitHub Desktop.
Protect plans older than sign up date minus one week.
<?php
/**
* Protect plans older than sign up date minus one week.
*
* This is for a site with new content published weekly;
* adjust as necessary for other publishing schedules.
*
* @since 1.0.0
*
* @author Travis Northcutt
*
*/
function gfmp_protect_old_meal_plans() {
// Do nothing if we're not on a singular post of the correct type
if ( !is_singular( 'meal-plan' ) ) return;
// how long user has been a member, in days
$days_as_member = mm_member_data( array( 'name' => 'daysAsMember' ) );
// publish date of the meal plan
$plan_date = get_the_date( 'U' );
// How many days the current plan has been published, rounded down
$days_as_plan = (int) floor( ( time() - $plan_date ) / 86400 );
// If plan was published before member joined (plus one week buffer), redirect
// to the built in Member Mouse error page.
if ( $days_as_plan > ( $days_as_member + 7 ) ) {
wp_redirect( "/mm-error/" );
exit;
}
return;
}
add_action( 'template_redirect', 'gfmp_protect_old_meal_plans' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment