Last active
March 11, 2022 17:59
-
-
Save LMNTL/dfd9dba3be2382626d126183f0cafa7a to your computer and use it in GitHub Desktop.
Restrict members from accessing members-only content using a checkbox in the profile. Requires PMPro and Register Helper Add On.
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
<?php | |
/* | |
Restrict members from accessing members-only content using a checkbox in the profile. | |
The button will only show when logged in as an admin. | |
Requires PMPro and Register Helper Add On to be installed, activated, and configured. | |
*/ | |
function my_pmprorh_init_restrict() | |
{ | |
//don't break if Register Helper is not loaded | |
if(!function_exists( 'pmprorh_add_registration_field' )) { | |
return false; | |
} | |
$fields = array(); | |
$fields[] = new PMProRH_Field( | |
'rh_restrict_access', // input name, will also be used as meta key | |
'checkbox', | |
array( | |
'label' => 'Restrict Access</br><strong>NOTE: the user will not be able to access members-only content until this option is disabled</strong>', | |
'profile' => 'only_admin', // only show in user profile for admins | |
) | |
); | |
foreach($fields as $field) | |
pmprorh_add_registration_field( | |
'checkout_boxes', | |
$field | |
); | |
//see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'init', 'my_pmprorh_init_restrict' ); | |
function my_pmpro_has_membership_access_filter($hasaccess, $mypost, $myuser, $post_membership_levels) | |
{ | |
$restrict = $myuser->rh_restrict_access; | |
if(!empty($restrict) && !empty($post_membership_levels)) | |
{ | |
$hasaccess = false; | |
add_filter("pmpro_non_member_text_filter", "my_pmpro_non_member_restricted_text"); | |
} | |
return $hasaccess; | |
} | |
add_filter('pmpro_has_membership_access_filter', 'my_pmpro_has_membership_access_filter', 10, 4); | |
function my_pmpro_levels_restricted_message($temp_content) | |
{ | |
$myuser = wp_get_current_user(); | |
$restricted = $myuser->rh_restrict_access; | |
if(!empty($restricted)){ | |
$message = '<div class="pmpro_message pmpro_error">'; | |
// Change this line to customize the message members see when their access is restricted | |
$message .= __("Your membership has been restricted. Please contact the site administrator for details.", 'paid-memberships-pro'); | |
$message .= '</div>'; | |
$temp_content = $message . $temp_content; | |
} | |
return $temp_content; | |
} | |
add_filter('pmpro_pages_shortcode_levels', 'my_pmpro_levels_restricted_message', 10, 1); | |
function my_pmpro_non_member_restricted_text() | |
{ | |
$message = '<div class="pmpro_message pmpro_error">'; | |
// Change this line to customize the message members see when their access is restricted | |
$message .= __("Your membership has been restricted. Please contact the site administrator for details.", 'paid-memberships-pro'); | |
$message .= '</div>'; | |
return $message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment