My workaroud to place a password protected download on my website is the following.
I placed the below code in my child themes functions.php.
In this code you first have to edit your desired password and the sdm_download id for your desired download.
Now when you place the shortcode [password_protected_download] in a text module (in my case in Divi) you will see an input field for the password and only when the password is correct the sdm download button is showing.
function password_protected_download_shortcode($atts) {
// Define the fixed password (change this to your desired password)
$fixed_password = 'ThisIsMyPassword'; // Change this to your desired password
// Variable to track if the password is correct
$is_password_correct = false;
// Check if the form has been submitted and if the password is correct
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['password'])) {
$submitted_password = sanitize_text_field($_POST['password']);
if ($submitted_password === $fixed_password) {
$is_password_correct = true;
}
}
// HTML content for the form or the result
ob_start();
if (!$is_password_correct) {
// Password is incorrect or hasn't been submitted yet
?>
<form method="POST" action="">
<label for="password">Wachtwoord: </label>
<input id="password" name="password" required>
<input type="submit" value="Verzend">
</form>
<?php
} else {
// Password is correct, render the shortcode
echo do_shortcode('[sdm_download id="####" fancy="1"]'); // WordPress shortcode
}
return ob_get_clean();
}
// Register the shortcode with WordPress
add_shortcode('password_protected_download', 'password_protected_download_shortcode');