Forked from strangerstudios/pmpro-redirect-non-members.php
Last active
May 10, 2021 09:34
-
-
Save JarrydLong/22b7c72ec2f2e488f8ced615e33d92cc to your computer and use it in GitHub Desktop.
Paid Memberships Pro Redirect Non-members to Login/Homepage
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 //Do not copy | |
/* | |
Redirect to login or homepage if user is logged out or not a member | |
Add this code to your active theme's functions.php file. | |
*/ | |
function my_template_redirect() | |
{ | |
global $current_user; | |
if( !function_exists( 'pmpro_login_url' ) ){ | |
return; | |
} | |
$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')); | |
//if the user doesn't have a membership, send them home | |
if(!$current_user->ID | |
&& !is_home() | |
&& !is_page($okay_pages) | |
&& !strpos($_SERVER['REQUEST_URI'], "login")) | |
{ | |
wp_redirect( pmpro_login_url()."?redirect_to=" . urlencode( $_SERVER['REQUEST_URI'] ) ); | |
} | |
elseif(is_page() | |
&& !is_home() | |
&& !is_page($okay_pages) | |
&& !$current_user->membership_level->ID) | |
{ | |
wp_redirect(home_url()); | |
} | |
} | |
add_action('template_redirect', 'my_template_redirect'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment