Last active
March 21, 2018 15:08
-
-
Save strangerstudios/c81d6bb91e3a188f259c to your computer and use it in GitHub Desktop.
Require a user meta field to access a category of posts with Paid Memberships Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Require conference user meta to view conference category posts. | |
Custom field is added via PMPro Register Helper Below. | |
Add all of this code to your active theme's functions.php or a custom plugin. | |
*/ | |
add_filter("pmpro_has_membership_access_filter", "hide_conference_posts2", 10, 4); | |
function hide_conference_posts2($hasaccess, $thepost, $theuser, $post_membership_levels) | |
{ | |
global $wpdb; | |
//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 | |
$conference = get_user_meta($theuser->ID, "conference", true); | |
if(empty($conference)) | |
$hasaccess = false; | |
} | |
return $hasaccess; | |
} | |
//add conference field | |
function my_pmprorh_init() | |
{ | |
//don't break if Register Helper is not loaded | |
if(!function_exists("pmprorh_add_registration_field")) | |
{ | |
return false; | |
} | |
$fields[] = new PMProRH_Field( | |
"conference", // input name, will also be used as meta key | |
"checkbox", // type of field | |
array( | |
"text"=>"I want to attend the annual conference. ($300/year)", | |
"memberslistcsv"=>true, | |
"profile"=>"admin_only", | |
)); | |
//add the fields into a new checkout_boxes are of the checkout page | |
foreach($fields as $field) | |
pmprorh_add_registration_field( | |
"checkout_boxes", // location on checkout page | |
$field // PMProRH_Field object | |
); | |
//that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action("init", "my_pmprorh_init"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment