Skip to main content

How to Limit File Downloads to Logged in Users in WordPress

How to Limit File Downloads to Logged in Users in WordPress

Do you want to learn how to limit file downloads to logged in users in WordPress? Offering downloads to logged in users only can help prevent unauthorized file sharing. It’s a great way to help get subscribers for your online store or membership site.

In this article, we’ll show you how to restrict downloads using the Easy Digital Downloads and WPCode plugins!

We’ll be following these four steps:

Step 1: Download and Install Easy Digital Downloads
Step 2: Add the File You Want to Restrict Access To
Step 3: Download and Install WPCode Plugin
Step 4: Insert the Force Login Code Snippet

Step 1: Download and Install Easy Digital Downloads

If you haven’t already, the first step is to download and install our Easy Digital Downloads plugin. This is a powerful tool for selling digital downloads. You can use it to offer a wide range of products, including unlimited files, eBooks, PDFs, and much more:

The WordPress and Easy Digital Downloads logos.

It’s trusted by more than 50,000 websites. Also, unlike other eCommerce plugins, EDD focuses specifically on helping you sell digital products. There are multiple plans to choose from so you can easily find the one that makes the most sense for your needs.

Once you select your EDD pass, you can download the plugin and upload it to your WordPress website. If you’re unfamiliar with this process, follow this simple step-by-step guide on installing plugins.

After uploading it to your site, click on Install Now, then Activate. This will add a Downloads item to your dashboard menu.

For more guidance on setting up EDD, you can refer to this guide: How To Easily Sell Digital Downloads

Step 2: Add the File You Want to Restrict Access To

Once you have EDD set up, you can start creating and uploading your digital products. If you haven’t already, now is a good time to upload the downloadable file to your WordPress site.

You can do this by going to Downloads » Add New:

Adding a new download using Easy Digital Downloads.

You can enter a file title, as well as a description. We’ll use an SEO eBook as an example:

A title and description for an eBook download.

To the right of the screen, you can also assign it a category, add a featured image, and more:

The Categories section of an EDD Download.

Next, you can set a price for your download by navigating to the Download Prices box located under the editor:

The Download Prices section of the EDD plugin.

There are a ton of features and settings you can apply to your downloadable product(s). We won’t get into all of them in this post. But you can refer to how to successfully launch your digital product to get started.

Step 3: Download and Install WPCode

For this next step, we’re going to insert a force login code snippet to your site. To make this quick and easy, you can use the WPCode plugin:

The WPCode WordPress plugin banner.

Previously known as Insert Footers + Headers, this free tool is used by over 1 million websites. It’s reliable, easy to use, and lets you add code snippets to your site without having to edit your theme files. It integrates seamlessly with WordPress and can make adding custom code to your site simple, even if you’re a beginner.

To add it, you can go to Plugins » Add New, then search for “wpcode”. It will be the first option. Click Install Now:

Installing WPCode in WordPress.

Next, click on Activate. Once installed and activated on your website, the plugin will add a Code Snippets menu item to your dashboard.

Step 4: Insert the Force Login Code Snippet

Once you have the WPCode plugin set up, the next step is to add your code. To limit file downloads to logged in users in WordPress, you can add the following:

<?php
/*
Snippet Name: Easy Digital Downloads - Force Login to Download
Description: Force users to be logged in to download purchased files through Easy Digital Downloads
Author: Easy Digital Downloads
Author URI: https://easydigitaldownloads.com/
Version: 2.0
*/
function eddwp_force_login_for_download( $download_id, $email, $payment, $args ) {
	if ( ! is_user_logged_in() ) {

		$login_url = wp_login_url( trailingslashit( home_url() ) . '?' . $_SERVER['QUERY_STRING'] );

		$message = sprintf( 'You must be logged in to download files. Login here.', $login_url );

		wp_die( $message, 'Error' );
	}
}
add_action( 'edd_process_verified_download', 'eddwp_force_login_for_download', 10, 4 ); 

Copy the code. Next, go back to WordPress and navigate to Code Snippets » Add Snippet:

The option to add a snippet with the Code Snippets WordPress plugin.

On this screen, you’ll find a wide variety of snippet types to choose from. Hover over the first option: Add Your Custom Code (New Snippet). Once you do, you can select Use snippet:

The option to add a custom code snippet with the Code Snippets plugin.

Next, you can add a title. We’ll use “Force Login”. In the Code Type drop-down menu, select PHP Snippet. Then, in the Code Preview box, paste the code you just copied:

The code that lets you limit file downloads to logged-in users in WordPress.

At the top of the screen, select the Inactive option to change it to Active. When you’re done, click on Save Snippet:

The option to save a code snippet in the Code Snippets plugin.

That’s it! Now when your users attempt to download a file, they’ll be asked to log into your site (if they haven’t already).

Conclusion: Limiting File Downloads to Logged in Users

Now you know how to limit file downloads to logged in users in WordPress. To add more content restrictions on your site, you can check out 9 expert ways to protect digital products (pros and cons).

Are you ready to start limiting file downloads on your site to only users logged into WordPress? Get our Easy Digital Downloads plugin to make doing so effortless!

Be sure to subscribe to our newsletter or follow us on Facebook and Twitter to get our latest resources for digital creators.

Casey Cavanagh

About Casey Cavanagh

Casey is a Technical Writer for Easy Digital Downloads at Awesome Motive.

2 comments

  1. Casey Cavanagh

    Hello there. Great first post Casey! I’m excited to see all the cool things people can do with WP Code. It will be interesting to see how WP Code and EDD can work together like this.

    Best of luck working on the EDD blog. Find me on LinkedIn (look for thisisjoewells) if I can ever help with a question…

    1. Casey Cavanagh

Leave a Reply

Your email address will not be published.