Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/d5b53fc5de1db15f728c8eebd9953f3f to your computer and use it in GitHub Desktop.
Save JarrydLong/d5b53fc5de1db15f728c8eebd9953f3f 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 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