1. Home
  2. Knowledge Base
  3. Advanced
  4. How to Hide Downloads for Non-Logged-In Users in Download Monitor

How to Hide Downloads for Non-Logged-In Users in Download Monitor

While Download Monitor offers various features for handling downloads, it does not natively provide an option to hide downloads exclusively for non-logged-in users. However, you can achieve this functionality by implementing a custom code snippet. In this documentation article, we will guide you through the process of hiding downloads for non-logged-in users using a code snippet.

Before you proceed, ensure that you have the following:

  • A WordPress website with Download Monitor installed and activated.
  • Administrator access to your WordPress dashboard.
  • Basic knowledge of working with WordPress and code snippets.

Step-by-Step Guide

Follow these steps to hide downloads for non-logged-in users using a custom code snippet:

1. Access Your WordPress Dashboard

Log in to your WordPress website’s dashboard using your administrator credentials.

2. Navigate to the Theme Editor

In the WordPress dashboard, go to “Appearance” and select “Theme File Editor.”

3. Edit the Functions.php File

In the Theme Editor, locate and select the functions.php file on the right-hand side. This file contains your theme’s custom functions.

4. Add the Custom Code Snippet

Insert the following custom code snippet at the end of the functions.php file:

function hide_download_buttons_for_guests($content) {
    if (!is_user_logged_in()) {
        $pattern = get_shortcode_regex(['download']);
        $content = preg_replace('/' . $pattern . '/s', '', $content);
    }
    return $content;
}
add_filter('the_content', 'hide_download_buttons_for_guests');

This code defines a function that checks if a user is logged in. If not, it uses a regular expression to find and remove [download] shortcodes from the content.

5. Save Changes

After adding the code snippet, click the “Update File” button to save your changes.

!Keep in mind – before making changes to any of the theme/plugin files do a back-up of your site.

6. Test The Results

Visit your website while logged in to ensure that you can still see and access the download links. Then, log out or open a private browsing window to test how the downloads are hidden for non-logged-in users.

Was this article helpful?

Related Articles

Start Protecting your WordPress Downloads

Protect and track downloads on WordPress... What are you waiting for?