Winter Sale! Save 20% on Pro Plugins & Books w/ code WINTER20
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.
Banhammer: Protect your WordPress site against threats.

Leave a reply

Name and email required. Email kept private. Basic markup allowed. Please wrap any small/single-line code snippets with <code> tags. Wrap any long/multi-line snippets with <pre><code> tags. For more info, check out the Comment Policy and Privacy Policy.

Subscribe to comments on this post

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 »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Thoughts
Working on a new pro WP plugin :)
📚 Fresh updates for all of my books now available! As always, book owners can download the latest versions for FREE :)
The same: your thoughts, your time, your money. Always in unison.
Google Broke My Heart is trending 😅
Ultimate block list to stop AI bots v1.8 now available, blocking over 700+ AI bots!
After eight months on the road, it is SO nice having my own office again.
Taking a break from the vanlife thing, now setting up an office in Washington state.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.