Custom Download Links

[ad_1]

On my site, I post lessons that are made of educational programming content. With each post, I create two downloadable files. One file contains all the individual scripts and the other contains the compiled game. These files are locked behind a paywall, requiring the user to purchase a specific membership to be able to download them.

I’m currently using the Download Monitor plugin to prevent my downloadable content from being publicly accessed and to restrict it to specific roles. Download monitor generates a custom link for each uploaded file, here’s an example of that link \`[https://domain.com/download/slug/?tmstv=1691378193]\`.

I have a sidebar widget that contains two buttons and is displayed alongside each lesson post. There is a Script & Game download button, which I update with the generated download links using some Javascript. Here is the code I use:

document.addEventListener(‘DOMContentLoaded’, function() {
const courseLink = Array.from(document.querySelectorAll(‘a.ld-primary-color’)).find(element =>
element.textContent.includes(‘Back to Course’)
);

if (!courseLink) {
console.log(“Course link not found.”);
return;
}

const courseName = courseLink.href.match(/courses\/([^\/]+)/)[1];
const pageTitle = document.querySelector(‘.entry-title’).textContent.trim();
const formattedCourseName = pageTitle.toLowerCase().replace(/\s+/g, ‘-‘);

const scriptsLink = document.querySelector(‘.scripts a’);
const gameLink = document.querySelector(‘.game a’);

scriptsLink.href = `https://domain.com/download/scripts_${courseName}_${formattedCourseName}/?tmstv=1691212910`;
gameLink.href = `https://domain.com/download/games_${courseName}_${formattedCourseName}/?tmstv=1691212910`;

});

The code is a bit ugly and variable names are a bit off, but I quickly threw it together in an attempt to see if it would work. This code does work, which is good, but I was wondering if anyone might have a better way to handle this.

The system feels unreliable, as one small change could break it all. I’m not sure how the \`tmstv=1691212910\` part of the link is generated, but if those became unique, then the system would be ruined.

I’ve uploaded only a few files and created only a few courses to test this with. My tests have worked fine, but again, I feel like there might be a better method for this.

I’ve tried creating a plugin and using the Download Monitor API to be able to get an array of all available downloads and then sort through them, but I had no success there. This seems a like it would be a bit more reliable. I’ve also tried looking into other download plugins, but it almost seems like Download Monitor is at the top of its niche…

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer