• I am working on a website for my client for pdf books. His requirement is to set a download limit for paid users. For example there are 300 books available on site. The paid member should be able to download any 100 books. After that he has to renew his membership. Please suggest any code snippet or free plugin. I cant afford paid plugin as i am giving website to the client on very low price.
    Humble request if anyone can help please.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Jay

    (@jaygumanid)

    There are a few ways you could approach this problem. Here are a couple of options:

    1. Use a plugin: One option is to use a plugin like WP-Download Manager, which allows you to set limits on the number of downloads allowed for different user roles. This plugin is free and can be installed from the WordPress plugin repository.
    2. Use custom code: Another option is to use custom code to track the number of downloads for each user and limit the number of downloads based on their membership status. To do this, you could use a combination of WordPress functions such as get_current_user_id() to get the current user’s ID, update_user_meta() to store the number of downloads for each user, and add_action() to track download events.

    Here’s an example of how you could use these functions to track and limit downloads:

    add_action( 'wp_footer', 'track_downloads' );
    
    function track_downloads() {
      // Check if the current user is logged in
      if ( is_user_logged_in() ) {
        // Get the current user's ID
        $user_id = get_current_user_id();
    
        // Get the current user's download count
        $download_count = get_user_meta( $user_id, 'download_count', true );
    
        // Increment the download count
        $download_count++;
    
        // Update the download count for the current user
        update_user_meta( $user_id, 'download_count', $download_count );
      }
    }
    
    // Function to check if the user has reached their download limit
    function has_reached_download_limit( $user_id ) {
      // Get the user's download count
      $download_count = get_user_meta( $user_id, 'download_count', true );
    
      // Check if the user has reached their download limit
      if ( $download_count >= 100 ) {
        return true;
      }
    
      return false;
    }

    You can then use the has_reached_download_limit() function to check if the user has reached their download limit before allowing them to download a book.

    I hope this helps! Let me know if you have any questions.

    Thread Starter bizglow

    (@bizglow)

    @jaygumanid thank you very much for your quick response, really appreciate, I will try both methods as advised and will update you

    ??

    Thread Starter bizglow

    (@bizglow)

    @jaygumanid well WP-Download manager was not free, or may be I am confused on it, anyhow can you please help to guide me if i want to set up 20 downloads per month for all the books/files on the WordPress site, this restriction will be for all user free and paid, but registered users.
    I will really appreciate if you can tell some easy way out ??

    Thread Starter bizglow

    (@bizglow)

    just to correct my statement, wp-download manager is free, but the downloads limit extension is not !

    Moderator bcworkz

    (@bcworkz)

    You could try out other download plugins. Searching “limit downloads” yields a lot of false positives, but there could be something in there that’s useful to you.

    You could modify the Download Manager plugin itself. If they are selling an extension, there is likely some hook in the core plugin that could be leveraged for the same purpose.

    Finally, you could custom code your own solution. Jay’s example is a good starting point, except you would want a better action hook. Hooking “wp_footer” actually limits page loads, not file downloads. You’ll need to find a more suitable hook, such as where the download link is offered to the user. Just preventing access to the link is fairly effective if the download URL cannot be easily guessed. A more secure scheme involves keeping downloads in a location inaccessible to the public, but accessible to PHP. On request, PHP verifies the user’s qualifications, then sends a file data stream to the user as the validated response.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Download limit on entire site for loggedin user’ is closed to new replies.