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

  • Lewis

    Hi there,

    I have tried to make a new ticket but just isn’t letting me fill in the form as everything is required but doesn’t drop down! See screencap

    Anyway, the problem I’m having is when I’m trying to assign a membership to a user.

    I login, go to the ‘All Members’ page, and try to assign the following memberships to each user:

    Username: usertype1 – Membership: Currently employed as a Racing Groom
    Username: usertype2 – Membership: Unemployed/returning to the industry
    Username: usertype3 – Membership: Looking for work experience and steps to become a Racing Groom

    If you try and assign those memberships, it all looks fine but they do not save. If you refresh the page they just all default to ‘Looking for work experience and steps to become a Racing Groom’ membership.

    Please could you have a look and let me know what could be causing this? I’ve disabled all plugins except wpmu/Membership2Pro and the same thing happens.

    Many thanks,

    Lewis

  • Ash

    Hello Lewis

    Thanks for enabling support access. I have found that the membership plugin was deactivated. So, I activated, checked and kept it deactivated again.

    When I tested, I can see the membership was assigned to the user, please check screenshot:

    So, is the issue fixed? Please let us know. Or if I checked in the wrong section, please feel free to suggest :slight_smile:

    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.