PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Display file download link if user is logged in

s2Member Plugin. A Membership plugin for WordPress®.

Display file download link if user is logged in

Postby webfed » June 21st, 2011, 6:28 am

Here is some code I used for my site, its not all that pretty but it works. Thought someone might find it useful.

Scope: “If a user is logged in at a certain level and the file exists in the download directory display a download link, if not display a link to the sign up page”

Step 1
Create a custom field in the post “Download File Name”, enter the name of the file in the value field

Step 2
Place the following code in your functions file (change yourdomain to your actual domain name)

Code: Select all
function check_file( $file_name, $member_level ) {

    $dir_name = "/home/public_html/content/plugins/s2member-files/access-" . $member_level . "/";
    if (file_exists($dir_name . $file_name)) {
    $return_url = "<a href=\"http://www.yourdomain.com/?s2member_file_download=access-" . $member_level . "/".$file_name."\">Download Template</a>";
    //$return_file = "0";
    } else {
    $return_url = "<a href=\"http://www.yourdomain.com/somelink/\">Download Options</a>";
    }
    echo $return_url;
}
 


Step 3
In the template file use the following (change yourdomain to your actual domain name)

Code: Select all
        <?php if (current_user_is("s2member_level4")){
                $member_level = "s2member-level4";
                $file_name = get_post_meta($post->ID, 'Download File Name', true);
                check_file($file_name, $member_level);
                
                
} else if (current_user_is("s2member_level3")){
                $member_level = "s2member-level3";
                $file_name = get_post_meta($post->ID, 'Download File Name', true);
                check_file($file_name, $member_level);
                
                
} else if (current_user_is("s2member_level2")){
                $member_level = "s2member-level2";
                $file_name = get_post_meta($post->ID, 'Download File Name', true);
                check_file($file_name, $member_level);
                
                
} else if (current_user_is("s2member_level1")){
                $member_level = "s2member-level1";
                $file_name = get_post_meta($post->ID, 'Download File Name', true);
                check_file($file_name, $member_level);
                
                
} else if (current_user_is("s2member_level0")){
                $member_level = "s2member-level0";
                $file_name = get_post_meta($post->ID, 'Download File Name', true);
                check_file($file_name, $member_level);
                
                
} else {
                echo "<a href=\"http://www.yourdomain.com/prices/\">Download Options</a>";
                }
                ?>

That should be it.
Last edited by Cristián Lávaque on June 21st, 2011, 11:33 am, edited 1 time in total.
Reason: Improve code readability. http://www.primothemes.com/forums/viewtopic.php?f=36&t=2780
User avatar
webfed
Registered User
Registered User
 
Posts: 4
Joined: June 12, 2011

Re: Display file download link if user is logged in

Postby Cristián Lávaque » June 21st, 2011, 11:54 am

Thanks for sharing it! :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron