Last active
December 4, 2020 16:01
-
-
Save UVLabs/20e9c84a4c02b7eed858c4b94e5aef81 to your computer and use it in GitHub Desktop.
Prevent logged out users from downloading WooCommerce downloadable product files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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