Membership 2 Pro - redirect instead of content protection message.

Instead of having a "The content you are trying to access is only available to members. Sorry." message I would like to redirect to my custom login page (https://+++.+++room.com/+++).

  • Ash

    Hello Lewis

    Please try the following code:

    add_action( 'ms_model_plugin_protect_current_page_before', function( $ms_model ){
    
        $new_redirect_link = 'http://wpmudev1.test/post-two/';
    
        if( defined( 'MS_PROTECTED_MESSAGE_REVERSE_RULE' ) && MS_PROTECTED_MESSAGE_REVERSE_RULE ) {
                $allowed_memberships = array();
                $memberships = MS_Model_Membership::get_membership_ids();
                foreach( $memberships as $membership_id ) {
                    $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
                    if( $membership->has_access_to_current_page() ) {
                        $allowed_memberships[$membership->priority] = $membership_id;
                    }
                }
                ksort( $allowed_memberships );
                $protected_membership_id = reset( $allowed_memberships );
            }
    
            // Admin user has access to everything.
            if ( $ms_model->member->is_normal_admin() ) {
                return;
            }
    
            $access = $ms_model->get_access_info();
    
            if ( ! $access['has_access'] ) {
    
                if ( $auth = filter_input( INPUT_GET, 'auth' ) ) {
                    //set cookie when mapped domains
                    $user_id = wp_validate_auth_cookie( $auth, 'auth' );
                    if ( $user_id ) {
                            wp_set_auth_cookie( $user_id );
    
                            wp_redirect( get_permalink() );
                            exit;
                    }
                }
    
                MS_Model_Pages::create_missing_pages();
                $no_access_page_url = MS_Model_Pages::get_page_url(
                    MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT,
                    false
                );
                //Get current page url
                $current_page_url = mslib3()->net->current_url();
    
                // Don't (re-)redirect the protection page.
                if ( ! MS_Model_Pages::is_membership_page( null, MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT ) ) {
                    if( defined( 'MS_PROTECTED_MESSAGE_REVERSE_RULE' ) && MS_PROTECTED_MESSAGE_REVERSE_RULE ) {
                        $no_access_page_url = esc_url_raw(
                            add_query_arg(
                                array( 'redirect_to' => urlencode( $current_page_url ), 'membership_id' => $protected_membership_id ),
                                $no_access_page_url
                            )
                        );
                    } else {
                        $no_access_page_url = esc_url_raw(
                            add_query_arg(
                                array( 'redirect_to' => urlencode( $current_page_url ) ),
                                $no_access_page_url
                            )
                        );
                    }
    
                    /*
                    $no_access_page_url = apply_filters(
                        'ms_model_plugin_protected_content_page',
                        $no_access_page_url
                    );
                    */
                    wp_safe_redirect( $new_redirect_link );
    
                    exit;
                }
            }
    
    }, 1 );

    You need to change this: $new_redirect_link = 'http://wpmudev1.test/post-two/';

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

  • Ash

    Hello Lewis

    Okay, then you may try the simpler process. Please try this code:

    add_action( 'template_redirect', function() {
    	global $post;
    	if( xxx == $post->ID )
    	{
    		wp_redirect( 'https://hub.racinggroom.com/login/' );
    		exit;
    	}
    } );

    Make sure you replace XXX with the protected page ID. The protected page is the page you selected in M2 settings.

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.