Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 13, 2021 18:36
Show Gist options
  • Save strangerstudios/0860cf05a227516fc53a to your computer and use it in GitHub Desktop.
Save strangerstudios/0860cf05a227516fc53a to your computer and use it in GitHub Desktop.
Require an addon package for access to a post category with Paid Memberships Pro and PMPro Addon Packages.
/*
Require an addon package for access to a post category.
Be sure to change the value of the addon package ID variable below.
Add all of this code to your active theme's functions.php or a custom plugin.
*/
//require a specific addon package to access posts in the conference category
add_filter("pmpro_has_membership_access_filter", "hide_conference_posts", 10, 4);
function hide_conference_posts($hasaccess, $thepost, $theuser, $post_membership_levels) {
global $wpdb;
//UPDATE THIS to the post ID of your addon package
$addon_package_post_id = 627;
//if PMPro Addon Packages is not installed, return
if(!function_exists('pmproap_hasAccess'))
return $hasaccess;
//if PMPro says false already, return false
if(!$hasaccess)
return false;
//if the post is in the conference category and the user has not purchased the conference addon package package, then block
if(has_category('conference', $thepost)) {
//make sure they purchased the addon package
if(!pmproap_hasAccess($addon_package_post_id, $theuser->ID))
$hasaccess = false;
}
return $hasaccess;
}
//show a link to checkout for the addon package on posts in the conference category
add_filter("pmpro_non_member_text_filter", "pmproap_pmpro_text_filter_for_conference_posts", 15);
add_filter("pmpro_not_logged_in_text_filter", "pmproap_pmpro_text_filter_for_conference_posts", 15);
function pmproap_pmpro_text_filter_for_conference_posts($text) {
global $wpdb, $current_user, $post, $pmpro_currency_symbol;
//UPDATE THIS to the post ID of your addon package
$addon_package_post_id = 627;
if(!empty($post)) {
if(has_category('conference', $post) && !pmproap_hasAccess($current_user->ID, $addon_package_post_id)) {
//which level to use for checkout link?
$text_level_id = pmproap_getLevelIDForCheckoutLink($addon_package_post_id, $current_user->ID);
//what's the price
$pmproap_price = get_post_meta($addon_package_post_id, "_pmproap_price", true);
//check for all access levels
$all_access_levels = apply_filters("pmproap_all_access_levels", array(), $current_user->ID, $addon_package_post_id);
//update text
if(!empty($all_access_levels)) {
$level_names = array();
foreach ($all_access_levels as $level_id) {
$level = pmpro_getLevel($level_id);
$level_names[] = $level->name;
}
$text = "<p>This content requires that you purchase additional access. The price is " . $pmpro_currency_symbol . $pmproap_price . " or free for our " . pmpro_implodeToEnglish($level_names) . " members.</p>";
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $addon_package_post_id) . "\">Purchase this Content (" . pmpro_formatPrice($pmproap_price) . ")</a> <a href=\"" . pmpro_url("levels") . "\">Choose a Membership Level</a></p>";
} else {
$text = "<p>This content requires that you purchase additional access. The price is " . pmpro_formatPrice($pmproap_price) . ".</p>";
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $addon_package_post_id) . "\">Click here to checkout</a></p>";
}
}
}
return $text;
}
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Sell Access to a Single Post Category Using the Addon Packages Plugin" at Paid Memberships Pro here: https://www.paidmembershipspro.com/sell-access-single-post-category-using-addon-packages-plugin/

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