Created
April 26, 2024 07:45
-
-
Save dwanjuki/46bebfeda6148e03ebe4ea68459126c9 to your computer and use it in GitHub Desktop.
Limit subsite access to specific membership levels
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 // copy from below | |
/** | |
* This recipe redirects members without the specified levels away from subsite pages | |
* to a page on the main network site | |
* | |
* This recipe has to be added to a customization plugin that is only active | |
* on a subsite where you would like to enforce this redirection (Not network activated) | |
* | |
* Read this companion article for step-by-step directions: | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/#method-2 | |
* | |
*/ | |
function my_pmpro_subsite_require_membership_level() { | |
// bail if not on a multisite env, or PMPro not active | |
if ( ! is_multisite() || ! function_exists( 'pmpro_hasmembershiplevel' ) ) { | |
return; | |
} | |
global $current_site; | |
$primary_network_site = $current_site; | |
$primary_network_site_url = get_home_url( $primary_network_site->id ); | |
/* | |
Set an optional landing page slug, | |
e.g. $landing_page_slug = '/membership-account/membership-levels/'; | |
*/ | |
$landing_page_slug = ''; | |
// set redirect url | |
$redirect_url = $primary_network_site_url . $landing_page_slug; | |
// membership level IDs allowed to access to this subsite | |
$allowed_levels = array( 1, 2 ); | |
if ( ! is_admin() && ! pmpro_hasMembershipLevel( $allowed_levels ) ) { | |
wp_redirect( $redirect_url ); | |
exit; | |
} | |
} | |
add_action( 'template_redirect', 'my_pmpro_subsite_require_membership_level' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment