Showing posts with label Membership. Redirect. Show all posts
Showing posts with label Membership. Redirect. Show all posts

Wednesday, April 26, 2023

Redirect Non Members to any page in pro Membership

 <?php function my_redirect_nonmembers() {

// Make sure PMPro is active.

if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {

return;

}

// Ignore members. Change to check for specific levels.

if ( pmpro_hasMembershipLevel() ) {

return;

}

global $post;


if( ! is_admin() && ! empty( $post->ID ) ) {

if( $post->post_type == "page" || $post->post_type == "podcast-video" ) {

//check if the user has access to the parent

if( ! pmpro_has_membership_access( $post->ID ) ) {

wp_redirect( pmpro_url( "levels" ) );

exit;

}

}

}

// Update this array.

$not_allowed = array(

"/members/",

"/groups/",

"/groups/create/",

);

// Get the current URI.

$uri = $_SERVER['REQUEST_URI'];

// If we're on one of those URLs, redirect away.

foreach( $not_allowed as $check ) {

if( strpos( strtolower( $uri ), strtolower( $check ) ) !== false ) {

// Go to levels page. Change if wanted.

wp_redirect( pmpro_url( 'levels' ) );

exit;

}

}

}

add_action( 'template_redirect', 'my_redirect_nonmembers' );

7 Must-Have WordPress Plugins to Boost Your Website in 2026

Want to make your WordPress site faster, smarter, and more powerful? Plugins are the secret weapon that can transform a basic blog into a pr...