Last active
April 3, 2021 03:34
-
-
Save kimcoleman/200bc75e55b7ea3a6b25cf92999fec2c to your computer and use it in GitHub Desktop.
Mark a member "paused" and deny access to members-only content protected by 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
<?php | |
/** | |
* Mark a member "paused" and deny access to members-only content. | |
* Show a message that the account is paused containing alink to the contact page. | |
* | |
* Requires Paid Memberships Pro and the Register Helper Add On. | |
* | |
*/ | |
function my_pmprorh_init_pause_access() { | |
// Don't break if Register Helper is not loaded. | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return false; | |
} | |
// Bail if not in administrative area. | |
if ( ! is_admin() ) { | |
return; | |
} | |
pmprorh_add_checkout_box( 'pause_hasaccess', 'Access to Member Content' ); | |
// Define the field. | |
$fields = array(); | |
$fields[] = new PMProRH_Field( | |
'pmpro_paused_user', | |
'checkbox', | |
array( | |
'label' => 'Pause User', | |
'text' => 'Deny Access to Member Content', | |
'profile' => 'admins', | |
) | |
); | |
// Add the fields into the checkout_boxes are of the checkout page. | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( | |
'pause_hasaccess', | |
$field | |
); | |
} | |
} | |
add_action( 'init', 'my_pmprorh_init_pause_access', 10, 1 ); | |
/** | |
* Deny access to member content if user is 'paused'. | |
*/ | |
function paused_member_pmpro_has_membership_access_filter( $access, $post, $user, $levels ) { | |
//if we don't have access now, we still won't | |
if ( ! $access ) { | |
return $access; | |
} | |
//no user, this must be open to everyone | |
if ( empty( $user ) || empty( $user->ID ) ) { | |
return $access; | |
} | |
//no levels, must be open | |
if ( empty( $levels ) ) { | |
return $access; | |
} | |
//now we need to check if the user is approved for ANY of the $levels | |
$paused_user = get_user_meta( $user->ID, 'pmpro_paused_user', true ); | |
if ( ! empty( $paused_user ) ) { | |
$access = false; | |
} | |
return $access; | |
} | |
add_filter( 'pmpro_has_membership_access_filter', 'paused_member_pmpro_has_membership_access_filter', 10, 4 ); | |
/** | |
* Show a different message for users that have their membership paused. | |
*/ | |
function paused_member_pmpro_non_member_text_filter( $text ) { | |
global $current_user, $has_access; | |
//get current user's level ID | |
$paused_user = get_user_meta( $current_user->ID, 'pmpro_paused_user', true ); | |
//if a user does not have a membership level, return default text. | |
if ( ! empty( $paused_user ) ) { | |
$text = __( '<p>Your membership is paused. Please contact us to reinstate your membership.</p><a href="/contact/">Contact Us</a>', 'paid-memberships-pro' ); | |
} | |
return $text; | |
} | |
add_filter( 'pmpro_non_member_text_filter', 'paused_member_pmpro_non_member_text_filter' ); | |
/** | |
* Filter the content of the Membership Account page to show message to paused user. | |
* | |
*/ | |
function paused_member_pmpro_membership_account_filter( $content ) { | |
global $pmpro_pages, $current_user; | |
// Check if current user is paused. | |
if ( is_user_logged_in() ) { | |
$paused_user = get_user_meta( $current_user->ID, 'pmpro_paused_user', true ); | |
} | |
if ( is_page( $pmpro_pages[ 'account' ] ) && ! empty( $paused_user ) ) { | |
$newcontent = __( '<div class="pmpro_content_message"><p>Your membership is paused. Please contact us to reinstate your membership.</p><a href="/contact/">Contact Us</a></div>', 'paid-memberships-pro' ); | |
$content = $newcontent . $content; | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'paused_member_pmpro_membership_account_filter', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Block or Pause a Specific Member’s Access to Restricted Content" at Paid Memberships Pro here: https://www.paidmembershipspro.com/block-pause-members-access-restricted-content/