Super Plugin Sale! Your Choice: BOGO or 30% Off »
Web Dev + WordPress + Security

Custom Headers with Simple Download Counter WordPress Plugin

Quick tip for my free WordPress plugin, Simple Download Counter. SDC is optimized for successful downloads in most server environments, but there always is an exception to the rule. For example, on some servers, a download’s content-length header may be calculated incorrectly for GZIP and possibly other file formats. This tutorial explains how to define your own headers for downloads when using Simple Download Counter, so you can dial in perfect downloads for any server configuration.

Contents

Note: This tutorial applies to Simple Download Counter version 1.8 or better.

Some Context

With Simple Download Counter, there are two basic types of downloads (as far as HTTP headers are concerned):

  • Local files
  • Remote files

So when you are adding new downloads to SDC plugin, you can choose local files that exist on your domain, or choose files that are hosted remotely on some other server. Each added download can be of either type.

In the plugin, all of the header action for local files happens in the file /inc/functions-core.php, beginning on line 326. The headers for remote files happens just after that in the code, beginning on line 356.

Note: These line numbers correspond to version 1.8 of the plugin. For other versions, the line numbers may be slightly different. If in doubt, search/find the two functions described below. That’s what we’re focusing on in this tutorial.

For each set of download headers (local and remote), there is a function that enables you to customize any aspect of the download headers. This is made possible by the following functions:

  • simple_download_counter_download_handler_local()
  • simple_download_counter_download_handler_remote()

These functions are not included in the plugin itself. You have to add them via your theme functions or simple custom plugin. So after you have added one or both of the functions to your WordPress site, Simple Download Counter will recognize and use the functions to define the HTTP headers and deliver the download file.

So that’s the basic context of what we’re doing here. Continue reading to learn how to define your own custom headers for SDC plugin.

How to: Custom Headers

To define custom headers for Simple Download Counter, follow these two steps.

Step 1: Add custom functions to WordPress

In your theme functions or simple custom plugin, create a function like this:

// custom headers for local downloads
function simple_download_counter_download_handler_local($id, $file_path) {
	
	// your custom header code goes here..
	
}

..and/or this function, for remote file downloads:

// custom headers for remote downloads
function simple_download_counter_download_handler_remote($id, $file_path, $current, $title) {
	
	// your custom header code goes here..
	
}

You can add one or both of the above functions to your WP site.

Step 2: Add code to your custom functions

Here is where you get to define your own custom headers. You can use the plugin’s default headers as a starting point. And then tweak as needed for whatever works with your server setup. To help get you going, let’s look at a couple of examples, one for local file headers, and another for remote file headers.

Local file headers

Let’s say that we want to customize the headers for local file downloads. We visit the plugin code around line 326 in /inc/functions-core.php. There we find the following if condition:

if (function_exists('simple_download_counter_download_handler_local')) {
	
	simple_download_counter_download_handler_local($id, $file_path);
	
} else {
	
	// default plugin headers found here..
	
}

We want to copy all the code that is contained in the else condition, where it says “default plugin headers found here”. There you will notice all of the default plugin headers and other codes. Copy all that and paste into your custom functions from Step 1, above. That will give you a solid starting point that is known to work on most servers. Then from there, you can tweak the headers as needed to work with your server configuration.

Remote file headers

Let’s say that we want to customize the headers for remote file downloads. We visit the plugin code around line 356 in /inc/functions-core.php. There we find the following if condition:

if (function_exists('simple_download_counter_download_handler_remote')) {
	
	simple_download_counter_download_handler_remote($id, $file_path, $current, $title);
	
} else {
	
	// default plugin headers found here..
	
}

We want to copy all the code that is contained in the else condition, where it says “default plugin headers found here”. There you will notice all of the default plugin headers and other codes. Copy all that and paste into your custom functions from Step 1, above. That will give you a solid starting point that is known to work on most servers. Then from there, you can tweak the headers as needed to work with your server configuration.

Wrap Up

In this tutorial, we learned how to define custom HTTP headers for file downloads in the Simple Download Counter WordPress plugin. It’s a bit involved to get set up, but it is a proven technique for dialing in whatever specific headers are required for downloads to work on just about any server that can run WordPress.

If you have any questions, feel free to reach out in the plugin support forum at WordPress.org, or reach us anytime via our contact form.

Cheers and happy downloading :)

About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
.htaccess made easy: Improve site performance and security.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
.htaccess made easy: Improve site performance and security.
Thoughts
All free plugins updated and ready for WordPress 6.6 dropping next week. Pro plugin updates in the works :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Crazy that we’re almost halfway thru 2024.
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.