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:
- Access Your WordPress Dashboard
Log in to your WordPress website’s dashboard using your administrator credentials. - Navigate to the Theme Editor
In the WordPress dashboard, go to “Appearance” and select “Theme File Editor.” - Edit the Functions.php File
In the Theme Editor, locate and select thefunctions.php
file on the right-hand side. This file contains your theme’s custom functions. - Add the Custom Code Snippet
Insert the following custom code snippet at the end of thefunctions.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');
- 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. - 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.