Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created January 23, 2012 15:50
Show Gist options
  • Save strangerstudios/1663916 to your computer and use it in GitHub Desktop.
Save strangerstudios/1663916 to your computer and use it in GitHub Desktop.
Restrict Access to WordPress Pages Within a Time Frame Using Paid Memberships Pro
/*
Functions to give access to certain pages within a timeframe.
Set the $pages and $levels arrays.
*/
function my_pmpro_has_membership_access_filter($hasaccess, $mypost, $myuser, $post_membership_levels)
{
$pages = array(214); // page ids
$levels[1] = array("2012-01-01", "2012-04-01"); // $level[level_id] = array(start_date, end_date);
$levels[2] = array("2012-02-01", "2012-05-01"); // date format is YYYY-MM-DD
$levels[3] = array("2012-03-01", "2012-06-01");
$levels[4] = array("2012-04-01", "2012-07-01");
$levels[5] = array("2012-05-01", "2012-08-01");
//only check if the current post is in pages array
if(in_array($mypost->ID, $pages))
{
global $wpdb;
//can only access this file based on the $levels array
$hasaccess = false;
//user's level
$users_level = $wpdb->get_var("SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $myuser->ID . "' LIMIT 1");
//what level to check?
$level = $levels[$users_level];
if(!empty($level))
{
//are we within the dates?
$rightnow = time();
if($rightnow >= strtotime($level[0]) && $rightnow <= strtotime($level[1]))
$hasaccess = true;
}
}
return $hasaccess;
}
add_action("pmpro_has_membership_access_filter", "my_pmpro_has_membership_access_filter", 10, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment