Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save femiyb/8d7b635eaa949f1ce1a2f24d98bf5f1f to your computer and use it in GitHub Desktop.

Select an option

Save femiyb/8d7b635eaa949f1ce1a2f24d98bf5f1f 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() {
// Check if a non-member is trying to access any other page besides the above or home page.
if ( is_page( 'events' ) && ! pmpro_hasMembershipLevel() ) {
wp_redirect( home_url('login') ); //redirect to home page. Change home_url() to home_url( '/page-slug' ) to redirect to specific page.
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