Skip to content

Instantly share code, notes, and snippets.

@UVLabs
Last active December 4, 2020 16:01
Show Gist options
  • Save UVLabs/20e9c84a4c02b7eed858c4b94e5aef81 to your computer and use it in GitHub Desktop.
Save UVLabs/20e9c84a4c02b7eed858c4b94e5aef81 to your computer and use it in GitHub Desktop.
Prevent logged out users from downloading WooCommerce downloadable product files.
<?php
function prefix_check_direct_download_link(){
$request = $_SERVER['REQUEST_URI'];
$parts = explode('&', $request);
// $parts[0] would be ?download_file=<product_id>
// We are assuming here... because that's how WooCommerce currently has the URL structured.
// A more full-proof way would be to use regex
$download_file_string = $parts[0];
if( ! is_user_logged_in() && strpos($download_file_string, '?download_file=') !== false){
$account_url = get_permalink( wc_get_page_id( 'myaccount' ) );
wp_redirect( $account_url );
exit;
}
// This wouldn't be ideal if you accept guest orders because they wouldn't have an account to log in...
// Add more checks as needed
}
add_action('woocommerce_init', 'prefix_check_direct_download_link');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment