Last active
August 8, 2023 02:44
-
-
Save indigetal/b32bae3eb33d5ad0733ded2d0b042fe6 to your computer and use it in GitHub Desktop.
Works with PMPro and the Multiple Memberships Per User Addon. Create 2 membership plan pages using Advanced Levels Page Shortcode Addon for 2 level groups, called group 1 and group 2 here. Redirects users to the relevant membership plan page if the user does not have the membership level that corresponds to the restricted page. Allows users to p…
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 | |
// Restrict access to certain pages based on level group: | |
function my_pmpro_custom_redirects() { | |
// The Level ID's associated with each "Level Group" | |
$group_one = array('1', '2', '3'); | |
$group_two = array('4', '5', '6'); | |
// The pages to be restricted | |
$page_names = array('group-1-access', 'group-2-access'); | |
// First check if PMPro is active | |
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) { | |
return; | |
} | |
global $pmpro_pages; | |
foreach ( $page_names as $page_name ) { | |
// Does the page name match? | |
if ( strpos( $_SERVER['REQUEST_URI'], '/' . $page_name . '/' ) !== false ) { | |
// Redirect members that don't have level ID's associated with level group 1 to the relevant plans page | |
if ( $page_name == 'group-1-access' && ! pmpro_hasMembershipLevel( $group_one ) && ( ! is_page( $pmpro_pages['levels'] ) || ! is_page( $pmpro_pages['checkout'] ) ) ) { | |
//The membership plans page listing the plans for Level Group 1 | |
wp_safe_redirect( '/group-1-plans-page/' ); | |
exit; | |
// Redirect members that don't have level ID's associated with level group 2 to the relevant plans page | |
} elseif ( $page_name == 'group-2-access' && ! pmpro_hasMembershipLevel( $group_two ) && ( ! is_page( $pmpro_pages['levels'] ) || ! is_page( $pmpro_pages['checkout'] ) ) ) { | |
wp_safe_redirect( '/group-2-plans-page/' ); | |
exit; | |
} | |
} | |
} | |
} | |
add_action( 'template_redirect', 'my_pmpro_custom_redirects' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do NOT name your levels in different level groups the same name, i.e. level group 1 - standard, pro, expert and level group 2 - standard, pro, expert. It will cause naming conflicts that break this redirect function.