Often membership sites provide some files to download. These can be zip, pdf, jpg and other files. We want these files to be available for download only for privileged users.

In our example we have two groups of members: Premium members and VIP members.
Both groups will be able to download some files but since the VIP membership is more expensive, VIP members will see and download more files than Premium members.

Compare the pictures.

What VIP members will see
What VIP members will see

What Premium members will see
What Premium members will see

What unprivileged users will see
What unprivileged users will see

Below we explain how you can implement this case on your WordPress site using Toolset plugins along with WordPress Download Manager (WDM) plugin – a free plugin from the WP repository that allows you to control file downloading for digital products.

With Toolset plugins you will:

  • Create custom roles (Toolset Access)
  • Create custom lists of files to download for each group of your members (Toolset Views)
  • Create a page that displays these lists based on the user role and which cannot be accessed by anybody else (Toolset Views & Access)
  • Create custom templates with a message for unprivileged users (Toolset Views & Access)

With the WordPress Download Manager plugin you will:

  • Add downloadable files to your WordPress.
  • Restrict access to these files to the custom roles you create with the Toolset Access plugin.


This page is a part of the How to build a membership site with WordPress series but you can follow this tutorial independently, no knowledge of previous parts is needed.

How to approach this task

The WordPress Download Manager plugin (WDM) stores the files to download as posts of the Downloads Custom Post Type.

WDM allows you to control access to files on two levels:

  • Post level
  • Category level

Post level control (1) is set individually for each post and has higher priority. Controlling access on the category level (2) allows you for massive post control.

We will choose option 2 to control access to our files.

In Part 1 we will create two categories:

  • VIP members – and associate the VIP member role with this category
  • Premium members – and associate the Premium member role with this category

For each Downalod post we will disable control on the post level and assign the post to one of these categories (or to both of them). The screenshot below summarizes what we will achieve.

Setting up Downloads - what we plan to achieve
Setting up Downloads – what we plan to achieve

Next, in Part 2, we will create a page that lists files to download according to the logged-in user role. We will also create a custom template for unprivileged users (as in the screenshots included at the beginning of this tutorial).

Part 1 – restricting access to files

Step 1 – creating custom roles

If you haven’t done so already, create custom roles:
Use the Toolset Access plugin.

Adding a custom role using the Toolset Access plugin
Adding a custom role using the Toolset Access plugin

  • Go to Toolset -> Access control.
  • Choose the Custom roles tab.
  • Use the “Add a new role” button to add the following two roles:
    • Premium member
    • VIP member

Copy privileges from Subscriber.

Step 2 – creating categories for Downloads

Creating categories for Downloads
Creating categories for Downloads

  • Go to Downloads -> Categories
  • Add a new category. Give it a name. We will name ours – VIP members.
  • On the same screen, In the Access section select the VIP member and Administrator roles.
  • Add another category. Give it a name: Premium members.
  • In the Access section select the Premium member and Administrator roles.

Step 3 – adding new files to download and setting access control

First, let’s add a few files to be available only for VIP members.

  • Go to Downloads -> Add New.
  • Give your post a name.
  • Use the Attach File section to upload your file.
  • Scroll to the Package section.
  • In the Access Control field delete “All Visitors”.
  • Optional: set the file Icon using the Icon tab.
  • Assign the post to the VIP members category.
  • Repeat steps 1-7 for other files you want to make available for VIP members only.

Adding new files to download and setting access control
Adding new files to download and setting access control

To add files that will be available both for VIP members and Premium members follow the same steps but assign the post to both categories: VIP members and Premium members.

Step 4 – checking if unprivileged users cannot download the file

Let’s check if restricting access works as we expect.
If a file to download has restricted access, you won’t see the Download link when you view your single post page.


Hint: Later on you will learn how you can create a custom template with your own message for unprivileged users

Testing as the site admin

First, let’s make the test being logged-in as the Administrator.
In that case you should be able to download the file.

Adminitrators will see the Download link
Adminitrators will see the Download link

    • Go to Downloads.
    • Pick a file that is assigned to only VIP members category.
    • View the file on the front-end.

You should see the Download link

  • Click it and the file download will begin.

Testing as a visitor

View the same post again, as a unprivileged user, you shouldn’t be allowed to download this file. How WDM works by default, you simply won’t see the Download link.

Unprivileged users won't see the download link
Unprivileged users won’t see the download link

  • Copy the file page url.
  • Logout or visit the page in another browser where you are not logged in.
  • Paste the link to the file page

Now the download link is missing. So unprivileged users won’t be able to see the file.

Repeat the same test being logged in as a user with the VIP member role and then again with the Premium member role. Compare your results.


Hint

Feel free to test a case when a user happens to know the direct link to the file but hasn’t an account (or sufficient role to download the file).

As you will see they won’t be able to download the file either. They will see the “You don’t have permission to download this file” message.

Part 2 – displaying lists of files available for the logged-in user

The files are secure.
Now, we need to create a page that displays all files available for the user who is currently logged-in. Depending on the user role that list will vary.

VIP members will see a list of all files. Premium members will see only some of them, those available for this group only. Compare the screenshots.

What VIP members will see
What VIP members will see

What Premium members will see
What Premium members will see

We will create two Views:

  • Courses for VIP members – this View will list all files from both categories: VIP members and Premium members
  • Courses for Premium members – this View will list files from the Premium members category only

Then, we will create a page that will display these Views conditionally, depending on the role of the currently logged-in user.

Step 1 – creating a View that list files to download for VIP members

Let’s get started with creating a View that lists all posts of the Downloadable CPT belonging to one of the VIP members and Premium members categories.

  1. Go to Toolset -> Views. Add a new View. Give it a name: Courses for VIP members
  2. Content Selection: Choose Posts -> Downloads
  3. Query Filter: Add a filter for the category field. Categories should belong to any the following: VIP members, Premium members.

    Adding filters for categories
    Adding filters for categories
  4. Loop Output Editor: create a Table sorted by columns
    • In the first column include the Post title. In our example we also include the file icon using the [wpv-post-field name=’__wpdm_icon’] shortcode (coming from WordPress Download Manager) and the post date.
    • In the second column include the download link using the [wpdm_direct_link id=’445751‘ label=’Download’] shortcode (coming from WDM)

    Adding the download link shortcode to the View output
    Adding the download link shortcode to the View output
  5. Save your changes.

We will add this View to a page in a while (see Step 3).

Step 2 – creating a View that lists files to download for Premium members

We need another View that looks exactly like the one we have just created but which lists the posts from the Premium members category only.

To save time, let’s copy the View we created in the previous step.

  • Go to Toolset -> VIews.
  • Locate the Courses for VIP members View (that’s the View you created in Step 1) and duplicate it.
  • In the Query filter section remove the VIP members category.


Hint: Instead of creating two similar Views you can create only one View and pass relevant categories in parameters.

Step 3 – creating a page that displays the Views based on user role

Now let’s create a page that will display one of our Views depending on the user role.

You will use the Toolset Access feature that allows you to display part of your page body conditionally – based on the user role.

  • If VIP member role -> display Courses for VIP members View
  • If Premium member role -> Courses for Premium Members View
  1. Go to Pages -> Add new. Give your page a name, for example: Courses to download.
  2. In the page editor click on the Access button.

    Using Access conditional-text feature
    Using Access conditional-text feature
  3. A dialog opens. Select Administrators and VIP members and choose the “Only users belonging to these roles will see the text” option. Insert the View that lists courses for VIP members.
  4. Click the Access button again to include the other View – Courses for Premium members. This time don’t include the Administrators.

    How to display a View based on a user role
    How to display a View based on a user role

Your page should look like this:

Page with toolset_access shortcodes to display Views based on roles
Page with toolset_access shortcodes to display Views based on roles

Both Views have been wrapped in toolset_access shortcodes that will display these Views only for users with appropriate roles.

Step 4 – creating a custom template for users without access

If you display your page as an anonymous user (or a user with any other role different than the admin, VIP member and Premium member) you will see only the page title.

Now, we would like to display a meaningful message followed by the login form instead. Please compare the screenshots below.

What unprivileged users will see - Before
What unprivileged users will see – Before

What unprivileged users will see - After
What unprivileged users will see – After

There are two ways you can handle this

Use the same conditional-text feature of Toolset Access you used previously, in Step 4, but this time with the deny operator:

[toolset_access role=”Administrator,Premium member,VIP member” operator=”deny”]
[wpv-post-body view_template=”members-only”]
[/toolset_access]

Inside your [toolset_access] shortcode you can include your message directly or insert a Content Template.

Use the post groups feature of Toolset Access plugin and assign a Content Template for users without access. Check the Restricting read access to specific pages tutorial for details.

In either case, you can fully control what unprivileged users will see.

Known issues

The Download CPT is recognized by the Views plugin but when you create a Content Template and assign to posts, it is not being used.

Solution
If you want to use Toolset to create custom templates for single-Download pages, please add the following code snippet to the functions.php file of your theme.

Change the prority of the wpdm_downloadable filter
add_action( 'init', function() {
	if( defined( 'WPDM_Version' ) ) {
		remove_filter( 'the_content', 'wpdm_downloadable' );
		add_filter( 'the_content', 'wpdm_downloadable', 0 );
	}
});

Then, use the following shortcode:

Post body used in Content Templates for Download CPT
[wpv-post-body view_template='None' suppress_filters='true']

Questions?

If you have any questions please not hesitate to post them on the Memebership Sites support page