☀️ Summer Sale! Save 35% on Pro Plugins & Books w/ code SUMMER
Web Dev + WordPress + Security

Micro API for Simple Download Counter WordPress Plugin

Simple Download Counter (SDC) version 2.3 features a very basic API that returns the number of times a specified file has been downloaded. For lack of a better name, I call it the “Micro API”. This tutorial explains how it works, and provides a real-world example showing how to use it.

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

Contents

Why?

I got an email from a user who asked for it. The person wanted to get file counts from simple URL requests.

I’m using your Simple Download Counter in WordPress and want to track how many downloads a file has received. I want to use it for an electronics project, where I want the download count to be displayed on an LED matrix driven by an Arduino. Is there a way to retrieve the download count for a specific file without having to access it via WordPress? Not a full API, that’ll be overkill, maybe just a URL I can ping and get a simple response with a number back?

So I thought why not add a simple “Micro API” that can do this..

How it works

First visit the plugin settings and enable the Micro API option. Please understand that enabling this option makes all of your download counts available to the public. No other data is sent via the Micro API, only a numeric value representing the number of times the specified file has been downloaded.

After enabling the Micro API option, you (or anyone) can get the download count for any file. To do so, make a request for a URL that looks like this:

https://example.com/?sdc-download-api=123

Replace example.com with your actual domain name, and replace 123 with the file ID for which you want to retrieve the download count. For example, if we want to know how many times the file with ID of 10 has been downloaded, we can enter the following URL in a browser, app, or script:

https://example.com/?sdc-download-api=10

This works for any file ID that is passed via sdc-download query-string parameter.

That’s all there is to it. Next we’ll look at a real-world example of how to make use of the Micro API..

Real-world example

And by “real-world” I mean “working code” :)

Let’s say that we have a PHP script where we want to “ping” the SDC Micro API to get download counts for our files. There are numerous ways of doing this, perhaps the easiest is to just call file_get_contents(), for example:

$id = 123;

$url = 'https://example.com/?sdc-download-api='. $id;

$count = file_get_contents($url);

echo filter_var($count, FILTER_SANITIZE_NUMBER_INT);

That will output the download count for file with ID of 123. You can call any ID, or use a function such as get_posts() to get just about any set of file IDs or even all file IDs. For example, to get all download file IDs:

$ids = get_posts(array(
	'post_type'      => 'sdc_download', // get IDs only for Simple Download Counter
	'fields'         => 'ids',          // get only IDs and no other data
	'posts_per_page' => -1              // get *all* file IDs
));

That will give you an array of all file IDs. So you can loop through them and get their download counts remotely via URL and the Micro API. You can also tweak the get_posts() arguments with a wide variety of useful parameters.

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
GA Pro: Add Google Analytics to WordPress like a pro.
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 »
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
Thoughts
The Office is Cheers with more irony.
More fine-grained control of macOS screen brightness: hold down Option + Shift before pressing either of the brightness keys. You get 64 increments instead of the usual 16.
REST Pro Tools featured in David McCan’s new video on locking down WordPress.
Gonna try exercise.
Launching my new plugin, REST Pro Tools 🛠️ Granular control of the WP REST API.
The algorithm is way too hypersensitive.
Working on a new pro WP plugin :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.