Skip to content

Instantly share code, notes, and snippets.

@arsalan13nov
Created August 31, 2017 00:16
Show Gist options
  • Save arsalan13nov/2b02da764dd0b4343464410042a6c880 to your computer and use it in GitHub Desktop.
Save arsalan13nov/2b02da764dd0b4343464410042a6c880 to your computer and use it in GitHub Desktop.
Snippet to add a shortcode that displays the media for download.
/**** Custom Shortcode to display media file download link. ****/
/**** Place the following code at the bottom of theme's functions.php file. ****/
/**** Sample Shortcode usage: [ld_download_media_file file_url="https://www.w3schools.com/html/mov_bbb.mp4" file_icon="fa-play-circle" file_title="Some File"]
/**
* Display the link of the media file for download.
*
* @param $atts - Shortcode attributes
* @return string
*/
function download_media_file_shortcode( $atts ) {
?>
<style type="text/css">
/* Download Link CSS */
div.file-class {
background: #f1f4f9;
border: 1px solid #6f9e94;
line-height: 2.8;
padding-left: 20px;
border-left: 0px solid;
border-right: 0px solid;
font-size: 18px;
}
a.file-link span {
padding-left: 15px;
}
a.file-link, div.file-class .fa, a.file-link:hover {
color: #75ceb9;
}
</style>
<?php
$file_url = $atts['file_url'];
$file_icon = $atts['file_icon'];
$browser = '';
$file_title = isset( $atts['file_title'] ) ? $atts['file_title'] : 'Download Now';
if ( isset($_SERVER['HTTP_USER_AGENT']) ) {
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if ( strlen( strstr( $agent, 'Firefox')) > 0) {
$browser = 'firefox';
}
if( empty( $browser ) )
$download_att = 'download="'.$file_title.'"';
else
$download_att = '';
return $content = '<div class="file-class">
<i class="fa '.$file_icon.'" ></i>
<a target="_blank" class="file-link" href="'.$file_url.'" '.$download_att.'>
<span>'.__( $file_title, 'download_media_file' ).'</span>
</a>
</div>';
}
add_shortcode( 'ld_download_media_file', 'download_media_file_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment