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' );

No comments:

Post a Comment

For Professional website contact WEB CODE ADDICT

View this post on Instagram A post shared by WebCodeAddict (@webcodeaddicted)