Page 1 of 1

Redirected From Restricted Content - Using Variables.

PostPosted: September 9th, 2011, 9:34 pm
by son0fhobs
I was hoping for an example in the documentation. Since there wasn't one, I thought I'd toss up mine in case it was helpful.

If a user is trying to access restricted content, they get forwarded to the page you choose in the options, with some variables in the query string.

Under the API / Scripting -> Membership Options Page Variables, it shows the custom links based on redirection. Documentation is great describing the variables in the links. Here's how I put it to use:

Code: Select all


<?php

// What level they need to be.  I'm not using this for now as I only have one premium level 
// $level_required = $_GET['s2member_level_req'];     


$url_attempt $_GET['s2member_seeking'];  // What page/post they were trying to access    
        
    
if($url_attempt){                                   // If it was a redirect
        
$url_attempt str_replace(" """$url_attempt);  //Ensure no spaces (shouldn't be issue)
        
$id_attempt explode("-"$url_attempt);            // Seperate post/page and ID
        

                // This would tell if you if post, page, or other  (I'm not using this for now)
        // echo 'page/post ', $id_attempt[0];  

        
if(is_numeric(end($id_attempt))){    // if page/post is a number - ID ?>
            
            <p> Sorry "<?php echo get_the_title(end($id_attempt)); ?>" is for members only.  Check out our awesome deals below to get access to the templates, along with tools, plugins, and other goodies!</p>
            <br />    
            <?php            
          
// Of course you might want to be more specific depending on what they were trying to access
        
}
    }    
?>


Hope it's of some assistance to someone.

Cheers!

PS Loving the amazing extensibility of the plugin and how incredibly thorough it is. Very well done!

Re: Redirected From Restricted Content - Using Variables.

PostPosted: September 10th, 2011, 2:28 am
by Cristián Lávaque
Thanks for the kudos! :)

And thanks for sharing that, I'm sure it'll help others trying to do something similar.

Re: Redirected From Restricted Content - Using Variables.

PostPosted: November 19th, 2011, 7:11 pm
by Raam Dev
Building upon the example by son0fhobs, I've created a similar solution to redirect visitors to a specific purchase page depending on the Specific Post/Page they were trying to access.

This code would need to be placed at the top of your theme's page.php file, or a separate Page Template for the Membership Options Page; using a Page Template is a much cleaner method, as you don't really want this code running for every single WordPress page.

The comments in the code below should explain how it works. Basically we detect when someone is being redirected to the Membership Options Page, determine which Specific Post/Page they were trying to access (using the page/post ID), and then redirect them to the correct page where they can purchase that Specific Post/Page with a Buy Now button (you'll need to create the purchase page for each Specific Post/Page and add the correct URLs and post/page IDs to the code).

Code: Select all

<?php
// Check if this is a redirect from a restricted page
if(isset($_GET['s2member_seeking'])) { $url_attempt = $_GET['s2member_seeking']; }
    if($url_attempt){
        // Extract the Specific Post/Page ID that the user is attempting to access
        $url_attempt = str_replace(" ", "", $url_attempt);
        $id_attempt = explode("-", $url_attempt);
        $id_attempt = $id_attempt[1];
        
        if
(is_numeric($id_attempt)){
                // Check if the Specific Page/Post ID is one that 
                // we want to redirect to its own Purchase Page.
                // You can add new case statements for every page
                // that you want to redirect. Make sure the case
                // statement ends with break;
                switch($id_attempt) {
                    case "12214": // Specific Page/Post ID
                        $purchase_page = "http://example.com/purchase-page-for-12214/"; // Purchase page
                        break;
                        
                    case 
"12315": // Specific Page/Post ID
                        $purchase_page = "http://example.com/purchase-page-for-12315/"; // Purchase page
                        break;                        
                        
                    default
: // Always leave this here
                        $purchase_page = "";
                        break;
                }
                
                
// If we need to redirect
                if(trim($purchase_page) != "") {
                    
                    
// Use JavaScript to redirect to the correct purchase page
                    ?>
                    
                    <script type="text/javascript">
                    <!--
                    window.location = "<?php echo $purchase_page; ?>";
                    //-->
                    </script>
                    
                    <?php
                
}
                
            
}
        } 
?>

Re: Redirected From Restricted Content - Using Variables.

PostPosted: November 21st, 2011, 3:47 am
by Cristián Lávaque
Very nice! :)

MOP vars have changed a bit lately, please check the documentation in the latest release. It should be simpler to code this now. WP Admin -> s2Member -> API / Scripting -> Membership Options Page Variables

I hope it helps.