Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ronalfy/e81cd6e112fb9f4b4671c51227c6e32a to your computer and use it in GitHub Desktop.
Save ronalfy/e81cd6e112fb9f4b4671c51227c6e32a to your computer and use it in GitHub Desktop.
Paid Memberships Pro - Level Page Landing Page Redirect
<?php
/**
* Redirect non-premium members to landing page.
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_temporary_restrict_access() {
// Ignore redirecting for non-logged in users. PMPro should take care of non-logged-in access.
if ( ! is_user_logged_in() ) {
return;
}
$premium_level_ids = array( 6 ); // 6 is the membership id. Can be 6,8,10, etc.
$has_membership_access_return_data = pmpro_has_membership_access( null, null, true ); // Can use to get membership level settings of current page
// $has_membership_access_return_data[1] is an array of level IDs that have access to this page.
if ( ! empty( $has_membership_access_return_data[1] ) && ! pmpro_hasMembershipLevel( $premium_level_ids ) ) {
wp_safe_redirect( esc_url( home_url( '/membership-account/membership-levels/' ) ) ); // Redirect to landing page.
exit;
}
}
add_action( 'template_redirect', 'my_pmpro_temporary_restrict_access' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment