Skip to content

Instantly share code, notes, and snippets.

@indigetal
Last active August 8, 2023 02:44
Show Gist options
  • Save indigetal/b32bae3eb33d5ad0733ded2d0b042fe6 to your computer and use it in GitHub Desktop.
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…
<?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' );
?>
@indigetal
Copy link
Author

indigetal commented Aug 4, 2023

To check for a single restricted page in the redirect conditional, use $page_name == 'group-1-access' in its parameters.

To check for more than one restricted page use in_array( $page_name, array('group-1-access', 'other-page') ) in the parameters of the same redirect conditional

To check for any level at all, remove the variable in the pmpro_hasMembershipLevel(). It will check for the existence of any Level ID.

To add another conditional with a different plans page, copy the elseif() conditional and paste it right beside the existing elseif() conditionals closing curly bracket }. In the conditional's parameters, change the $page_name == value you are checking for, the variable in pmpro_MembershipLevel(), and the path to the new plans page in wp_safe_redirect() as needed.

@indigetal
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment