Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active November 18, 2022 00:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewlimaza/e9a10c45a869f1ecafe819f3bdb2f2e0 to your computer and use it in GitHub Desktop.
Save andrewlimaza/e9a10c45a869f1ecafe819f3bdb2f2e0 to your computer and use it in GitHub Desktop.
Restrict all pages except home and Paid Memberships Pro pages for non-members.
<?php
/**
* This will restrict all pages except Paid Memberships Pro pages or the home page of your website to non-members / non-approved members / logged-out users.
* This won't affect administrators.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_redirect_non_members() {
global $pmpro_pages;
if( is_page( $pmpro_pages ) || is_home() || current_user_can( 'manage_options' )) {
return;
}
$access = false;
global $current_user, $pmpro_pages;
$user_id = $current_user->ID;
if( !empty( $user_id ) ) {
// Get approval status
if( class_exists( 'PMPro_Approvals') ) {
$approval = PMPro_Approvals::getUserApproval( $user_id );
// User currently does not need approval.
if ( empty( $approval ) ) {
return;
}
$approval_status = $approval['status'];
if ( ! empty( $approval_status ) && $approval_status != 'approved' ) {
$access = false;
} else {
$access = true;
}
}
// Make sure logged-in non-members don't have access.
if( ! pmpro_hasMembershipLevel() ) {
$access = false;
}
}
// if the user is not approved, redirect to BuddyPress restricted page or home page if add on not enabled.
if ( ! $access ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'template_redirect', 'my_pmpro_redirect_non_members', 45 );
@lucasmancan
Copy link

Hi Andrew, aparentilly the class PMPro_Approvals does not exist in the current version, do you have any alternative to it?

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