Hello guys welcome back to my blog therichpost.com. Guys today in this post I will tell you How to Restrict PDF Access After Subscription Expiry in WooCommerce (With Code)?
Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:
Guys If you’re running a WooCommerce subscription-based website and offering premium PDFs, documents, or downloadable resources, you may have noticed a serious issue:
Users can still access PDFs even after their subscription expires or is cancelled.
Use Case
- User purchases a monthly / yearly subscription
- Subscription gives access to PDF files
- PDFs appear in My Account → Downloads
- After subscription expires / cancels
- PDFs are still visible and downloadable
We’ll fix this properly and securely.
Correct Approach (Best Practice)
Instead of deleting PDFs or hiding products manually:
- Check subscription status
- Allow access only if subscription is ACTIVE
- Automatically block access when expired
Guys now we will do this with code snippet and here is that:
Step 1: Check If User Has Active Subscription
Add the following code to your theme’sfunctions.php file or a custom plugin:
function user_has_active_subscription() {
if (!function_exists('wcs_user_has_subscription')) {
return false;
}
return wcs_user_has_subscription('', '', 'active');
}
What this does:
- Checks if the logged-in user has any active subscription
- Returns
trueorfalse
Step 2: Remove PDF Access After Subscription Expiry
Now add this code below the first function:
add_filter('woocommerce_customer_get_downloadable_products', function ($downloads) {
if (!user_has_active_subscription()) {
return [];
}
return $downloads;
});
The Downloads section becomes empty automatically when the subscription ends.
Optional: Block Direct PDF URLs (Recommended)
If someone saves the direct PDF link, block it too:
add_action('template_redirect', function () {
if (is_account_page() && is_user_logged_in()) {
if (!user_has_active_subscription()) {
if (isset($_GET['download_file'])) {
wp_die('Your subscription has expired. Please renew to access this file.');
}
}
}
});
Why This Method Is Best
- No need to delete files
- Works with renewals automatically
- Secure and scalable
- Professional subscription handling
- No UI or HTML changes required
Final Result
After applying this solution:
- PDFs disappear from My Account when subscription expires
- Renewing subscription restores access instantly
- Your content stays secure
- Your subscription system behaves professionally
Need More?
If you want:
- Role-based PDF access
- Page-level subscription restriction
- PDF streaming (no download)
- Email reminders before expiry
- OTP login + subscription access
Just tell me — happy to help
Guys if you will have any query then feel free to contact me via contact us page.
Ajay
Thanks
