Get WPFTS Pro today with 25% discount!

Protect attachment file from downloading (without plugins)?


  • Well, lots of WPFTS Pro users asked: how we can prevent open/download attachment (PDF, DOCX) files from non-authorized users? They want files to be found by their content and then shown in search results. But when the non-authorized user trying to open this file, he should be redirected to the special page where he can log in or buy access, etc.

    The solution is pretty simple and only contains a dozen lines of PHP code.

    The idea is to show "attachment page" links instead of direct links to files in search results (you can get this using the respective checkbox in WPFTS Settings). And then replace this attachment page with the simple script which is checking user rights and make a proper action: whether redirection to the "login page" or outputs the file data without disclosing the actual file path.

    First, we need to find the single-attachment.php file in the root of your current theme (or better, child theme).

    In case the file does not exist, we need to create an empty one.

    Next step, we should put the code in this file like this:

    <?php
    
    global $post;  // Current post (attachment)
    
    // Check user rights
    // Note: this piece can be different depending on the user access management plugin which you're using
    $is_allowed = false;
    if (is_user_logged_in()) {
        // User is logged in
        $user = wp_get_current_user();
    
        // Check the user rights
        if (in_array('customer', $user->roles) || in_array('administrator', $user->roles)) {
            // User is a customer or admin
            $is_allowed = true;
        }
    }
    
    if ($is_allowed) {
        // Output file data
        $fn = get_attached_file($post->ID);
    	
        if (is_file($fn) && file_exists($fn)) {
            $mime = mime_content_type($fn);
    
            header('Content-Type: '.$mime);
            
            //header('Content-Disposition: attachment; filename="'.basename($fn).'"');	// Download
            header('Content-Disposition: inline; filename="'.basename($fn).'"');	// Open inline
    
            readfile($fn); // Passthrou the file data
        } else {
            // File error
            header('HTTP/1.0 404 Not Found');
            header('Content-Type: text/plain');
    
            echo 'This file is not available. Please check the link.';
        }
    			
    } else {
        // Show "not enough rights" message or redirect to the proper page
    
        // Case 1: Show the message
        //header('Content-Type: text/plain');
        //echo 'Not enough rights to download this file. Please log in and ensure that you have proper license.';
        
        // Case 2: Redirect to the "payment page" or "login page", whatever you need
        header('Location: /wp-login.php&redirect_to='.urlencode($_SERVER['REQUEST_URI']));
    }
    exit();
     
    

    This is a sample code, which means that you need to change some pieces for your exact website.

    Hope it's understood. If you have questions - ask below. Thanks!

Suggested Topics

  • 0 Votes
    5 Posts
    609 Views
    G

    @EpsilonAdmin Thanks for quick response and question.
    Yes, my hope was to use the standard WP search widget - but I haven't explored any other option. If there's a better way I'm happy to get guidance 😀

    My site has been recently re-created in WordPress after quite a few years of running under Joomla and that Joomla installation had a free plugin called jiFiles (?) which did the document scanning/indexing. A standard search, scoped on file name or a string from within file content, would pull up a list of file names each hyperlinked to the file itself to easily click on for in-browser viewing (or possibly downloading).

    I appreciate that there are other WordPress plugins that offer a full document management system but they have a much larger feature set than I need and are also majorly expensive for a small non-profit community web site.

  • 0 Votes
    1 Posts
    385 Views
    No one has replied
  • Searching data attached to image files

    Recipes and Known Solutions
    2
    1 Votes
    2 Posts
    1k Views
    EpsilonAdminE

    You can upload images to the WP Media Library as well and add the image description to the image. WPFTS Pro can search these images by the description then.

    Alternatively, you can create a special post type and upload images as "Featured Image" to each of these posts, and put the description and special information to the post_content or additional meta field(s) - depending on the number of data fields and type of information.

    In both cases, WPFTS Pro will help you with the search.

  • 0 Votes
    1 Posts
    655 Views
    No one has replied
  • 0 Votes
    3 Posts
    828 Views
    EpsilonAdminE

    Yes, you can do that. But it will need some coding.

    First of all, we need to extract a PDF link or file path from the post. It should be done at indexing stage, inside the 'wpfts_index_post' hook function.

    After the link/path extraction, we should read this file and extract the text information from it. It still should be done inside the same hook.

    Okay, when we have the text, we put it to one of the index clusters (your preference) and let it to the indexer to make its job as well.

    The final code will depend from the way how you have attached the PDF file to the parent post. It can be done using very different ways.

    Please give me some more explanation of this and I will post the semi-ready code here.

Be the first to read the news!

We are always improving our products, adding new functions and fixes. Subscribe now to be the first to get the updates and stay informed about our sales! We are not spammy. Seriously.

Join Us Now!

We are a professional IT-team. Many of us have been working in a Web IT field for more than 10 years. Our advanced experience of software development has been employed in the creation of the WordPress FullText Search plugin. All solutions implemented into the plugin have been used for 5 or more years in over 60 different web-projects.

We are looking forward to your comments, requests and suggestions in relation to the current plugin and future updates.

ewm-logo-450

The forum powered by NodeBB | Contributors