Forked from andrewlimaza/my-template-redirect-example.php
Created
November 14, 2022 06:13
-
-
Save JarrydLong/d5b53fc5de1db15f728c8eebd9953f3f to your computer and use it in GitHub Desktop.
Redirect non-members to homepage but allow access to PMPro checkout pages.
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 | |
/** | |
* Redirect non-members (including logged-in non-members) away from restricted pages and to login 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() { | |
$okay_pages = array(pmpro_getOption('billing_page_id'), pmpro_getOption('account_page_id'), pmpro_getOption('levels_page_id'), pmpro_getOption('checkout_page_id'), pmpro_getOption('confirmation_page_id')); | |
// 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( wp_login_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