Display file download link if user is logged in
Posted: 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)
Step 3
In the template file use the following (change yourdomain to your actual domain name)
That should be it.
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.