For those who want maximum control over secure bypass links in WordPress, you can implement custom signed URLs using code.
This method is perfect if you don’t want to rely on plugins or need very specific features, such as integrating links with your custom workflows, tracking usage, or restricting access by IP.
Step 1: Generate a Secure Link
The first step is to create a function that generates a signed URL using a secret key and an expiration timestamp. This ensures that the link cannot be tampered with and is only valid for a limited time.
function generate_secure_link($path, $expiry) {
$secret = ‘YOUR_SECRET_KEY’;
$signature = hash_hmac(‘sha256’, $path . $expiry, $secret);
return home_url($path) . “?exp={$expiry}&sig={$signature}”;
}
// Example usage:
// Generates a link valid for 1 hour
echo generate_secure_link(‘/private-page’, time() + 3600);
How it works:
- $secret is your private key stored on the server.
- $expiry is a UNIX timestamp specifying when the link should expire.
- hash_hmac() generates a cryptographic signature that verifies the link’s authenticity.
Step 2: Validate the Token
Next, you need to check the signed link every time someone accesses the page or file. This ensures that only valid requests are allowed.
function validate_secure_link() {
if (!isset($_GET[‘exp’], $_GET[‘sig’])) return;
if (time() > (int)$_GET[‘exp’]) wp_die(‘This link has expired.’);
$path = strtok($_SERVER[‘REQUEST_URI’], ‘?’);
$secret = ‘YOUR_SECRET_KEY’;
$valid_sig = hash_hmac(‘sha256’, $path . $_GET[‘exp’], $secret);
if (!hash_equals($valid_sig, $_GET[‘sig’])) wp_die(‘Invalid or tampered link.’);
}
add_action(‘template_redirect’, ‘validate_secure_link’);
Explanation:
- The function checks if the link is expired.
- It validates the signature to prevent tampering.
- If the link is invalid or expired, it displays a friendly error message instead of exposing the content.
Optional Enhancements for Advanced Security
- Track Usage Count: Keep a log of how many times each link is accessed to enforce single-use or limited-use links.
- Restrict by User Session or IP: Only allow access from specific IP addresses or logged-in sessions for extra control.
- Admin Revocation Toggle: Create a backend option to instantly revoke links if they are leaked or misused.
- Logging & Analytics: Track link clicks and download events for monitoring and reporting.
Using custom signed URLs, you get full control over how and when your content is accessed. This method is particularly useful for developers managing sensitive client files, premium downloads, or private course materials, where plugins might not offer the exact level of flexibility you need.
Leave a Reply