Skip Navigation

[Resolved] Only allow logged in users to download files

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 15 replies, has 2 voices.

Last updated by imaneK 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#622425
Capture6.PNG
Capture5.PNG
Capture4.PNG
Capture3.PNG

Hi there,

I am creating a website that allows users to upload resources for other users to download. In the (resource) content template, I would like to allow logged out users to be able to view the available resources but the files should only be downloadable by logged users.
This answer was very useful..
https://toolset.com/forums/topic/only-allow-logged-in-users-to-download-files/

However, I am already using short code to display the uploaded resources( as seen in screenshots). So, I'm not to sure how to integrate both together. Information on on the short codes creates can be found here...
https://toolset.com/forums/topic/short-code-to-allow-users-to-view-the-uploaded-files-name-size-and-type/

Screenshots are provided for clarification.

Support would be very appreciated.

Thank you.

#622444

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

You have two options, the first of which isn't dependent on Toolset, the second of which uses Toolset Access.

You are generating that output with a custom shortcode. You can:

1. edit the shortcode and in the section that displays the file name as a link add a test for whether the user is logged in and if not display the title but not as a link. Use the standard WP function is_user_logged_in(). The first example in the documentation shows using and if then else structure much like you would need: https://developer.wordpress.org/reference/functions/is_user_logged_in/

2. if you are not comfortable editing that shortcode so much you would need to make an almost-duplicate version of the shortcode and change the part that outputs the title as a link to just display the title.

Then where you insert the shortcode currently, use wpv-conditional shortcodes to alternately display one or the other depending on whether the user is logged in or not. You can register the same function is_user_logged_in to use inside your conditional shortcodes, as described here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

Alternatively, if you are using Access, you can use Access shortcodes to make one version of the shortcode visible to guests and another to logged-in users with specified roles: https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/

#622559

Ok, thank you very much for your reply.

I think I will go with the second option so that I may have more flexibility. However, I am not too sure which part of the code to copy and which to change. Also, can I have a little more guidance with the different shortcodes to use (the one with just the file name, and the one with the file link).

Thank you very much for your help.

#622613

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Copy the entire code, the add_shortcode line and then the wpml_hard_link function that follows, and paste it below.

Name this copy of the wpml_hard_link function something else, e.g. wpml_no_link, and change the line that registers the shortcode so that it uses a different shortcode name (e.g. 'my-file-name2') and references the new name you just chose for the function.

Locate the line that has the comment "Create the link and store it in the $content variable" and change it so that it doesn't include the link:

$content .= $filename[0];

Now you have two shortcodes, my-file-name which outputs the links, and my-file-name2 (or whatever you called it) which outputs the same but displays just the file name not the link.

At Toolset > Settings > Front-end Content register the function is_user_logged_in to be used in wpv-conditional shortcodes as described previously.

Then update where you have currently inserted your custom shortcode and insert both, wrapped in conditional shortcodes, something like:

[wpv-conditional if="( is_user_logged_in() eq '1' )"]
...Shortcode for when user is logged in
[/wpv-conditional]

[wpv-conditional if="( is_user_logged_in() eq '0' )"]
...Shortcode for when user is not logged in
[/wpv-conditional]
#622715
Capture11.PNG
Capture10.PNG
Capture9.PNG

Thank you for your assistance.

I followed the steps to the best of my ability. However, the shortcode my_file_name2 does not seem to be working.
Screenshots are provided for more clarity.

Thank you.

#622844

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

In your screen capture9 you define the shortcode like so:

add_shortcode( 'my_file_name2', 'wpml_no_link' );

What that does is register a shortcode called 'my_file_name2' and says that the function that will do the work of the shortcode is called 'wpml_no_link'.

Do you have a function called 'wpml_no_link'?

Your next line defines a function called wpml_hard_link.

#622935
Capture12.PNG

I changed the function from wpml_hard_link to wpml_no_link. But there seems to be no difference.

#622941

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I can't really tell from your screenshots what the problem is.

Do you want me to take a look?

How/where have you added the code to register the shortcodes?

On what page or template are you using them?

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to break anything.

#622944

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

How/where have you added the code to register the shortcodes?

On what page or template are you using them?

#622998

I am using a content template named resources. The template is being edited with the divi builder.

#623002

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

How/where have you added the code to register the shortcodes?

#623034
Capture16.PNG
Capture15.PNG
Capture14.PNG
Capture13.PNG

Please see the attached screenshots.

Thank you 🙂

#623041

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Yes, I understand that, but where have you added the code?

It is normally in the child theme's functions.php file or a plugin, but I couldn't find it, so I'm wondering where you added it.

#623219

Hi, I'm really sorry about that.

The code was added to the Divi theme's functions.php

#623247

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Found it, thank you.

You had made a mistake copying and pasting the original shortcode, the bracket to close the first shortcode function was left trailing after the second shortcode declaration.

I fixed that and it appears to work okay now.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.