Hi,
is there a way to force a download of the linked media file using the button element rather than opening [PDFs, MP3s and MP4 for example] in new tabs?
Thanks in advance.
Hey tebitron gmbh,
Thank you for the inquiry.
Apply a custom css class name (av-download-button) to the button element in the Advanced > Developer Settings, then add this code in the functions.php file to apply the download attribute to the button element.
// custom script: create download button
function ava_custom_script_download_link()
{
?>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('.av-download-button').attr('download', true);
});
})(jQuery);
</script>
<?php
}
add_action( 'wp_footer', 'ava_custom_script_download_link', 9999 );
// https://www.w3schools.com/tags/att_a_download.asp
Best regards,
Ismael
Yes – but custom-class goes to the parent container of the a.avia-button – the download attribute had to be on the anchor itself – and setting the attribute download to true will end up in downloads true.pdf , true.jpg etc
so use:
function ava_custom_script_download_link()
{
?>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('.av-download-button').each(function() {
$(this).find('.avia-button').attr('download', '');
});
});
})(jQuery);
</script>
<?php
}
add_action( 'wp_footer', 'ava_custom_script_download_link', 9999 );
Thanks! Works perfectly.
Hi,
Glad Ismael could help you out!
Let us know if you have any other questions and enjoy the rest of your day :)
@guenni007 thanks for your help as always!
Best regards,
Yigit