Skip to content

Instantly share code, notes, and snippets.

@greathmaster
Created October 13, 2015 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save greathmaster/0608b216fe916cc7d80d to your computer and use it in GitHub Desktop.
Save greathmaster/0608b216fe916cc7d80d to your computer and use it in GitHub Desktop.
Only show levels on levels page which the original referral page belonged to.
/*
Only show levels on level page which the original content belonged to. For example if a site has 3 levels: Bronze, Silver, Gold and
a post is restricted to Silver and Gold, clicking on the "Register" button will lead them to the levels page showing only Silver and Gold levels.
To use: employ the shortcode [my_level_link] under Memberships >> Advanced >> Message for Logged-out Users (or Message for Logged-in Non-members)
Ex:
This content is for !!levels!! members only.<br /><a href="http://example.com/wp-login.php">Log In</a> <a href="[my_level_link]">Register</a>
*/
function my_level_link()
{
global $post;
$hasAccess = pmpro_has_membership_access($post->ID, null, true);
$access = site_url()."/membership-account/membership-levels/?";
foreach($hasAccess[1] as $key => $value)
{
$access = $access."levels[]=".$value."&";
}
return $access;
}
add_shortcode('my_level_link', 'my_level_link');
function my_pmpro_levels_array($levels)
{
$allowed_levels = $_REQUEST['levels'];
$newlevels = array();
foreach($levels as $level)
{
if(in_array($level->id, $allowed_levels))
$newlevels[] = $level;
}
return $newlevels;
}
add_filter("pmpro_levels_array", "my_pmpro_levels_array");
@kimwhite
Copy link

Forked to add a check if going to levels page from a direct link no on a protected page https://gist.github.com/kimwhite/2ff0eff060badbbebbddfcf20cc74269

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