Skip to content

Instantly share code, notes, and snippets.

@kimwhite
Forked from greathmaster/pmpro-restrict-levels.php
Last active October 24, 2022 19:06
Show Gist options
  • Save kimwhite/2ff0eff060badbbebbddfcf20cc74269 to your computer and use it in GitHub Desktop.
Save kimwhite/2ff0eff060badbbebbddfcf20cc74269 to your computer and use it in GitHub Desktop.
Only show levels on levels page which the original referral page belonged to.
<?php // do not copy this line.
/**
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
/*
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)
{
if ( empty( $_REQUEST['levels'] ) )
{ return $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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment