Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active January 10, 2023 14:03
Show Gist options
  • Save andrewlimaza/60d8057d15fc270cba4c27c5320c7203 to your computer and use it in GitHub Desktop.
Save andrewlimaza/60d8057d15fc270cba4c27c5320c7203 to your computer and use it in GitHub Desktop.
Redirect non-members to homepage but allow access to PMPro checkout pages.
<?php
/**
* Redirect non-members (including logged-in non-members) away from restricted pages and to home page.
* This allows non-members to access Paid Memberships Pro checkout/levels pages as well as the default WordPress login page.
*
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_redirect_non_members_example() {
global $pmpro_pages;
$okay_pages = $pmpro_pages;
// Check if a non-member is trying to access any other page besides the above or home page.
if ( ! is_front_page() && ! is_page( $okay_pages ) && ! pmpro_hasMembershipLevel() && ! strpos( $_SERVER['REQUEST_URI'], 'login' ) ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'template_redirect', 'my_redirect_non_members_example' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment