Image

Using Wordpress Shortcode To Create Beautiful Download Boxes

January 7, 2009

Download
Since shortcode was introduced in Wordpress 2.5 it’s been possible to do many cool things. Everything from simple donate buttons to more advance things. In this article I will show you a simple but efficient way to create one of those download-boxes that I and many other use on their blogs.

The shortcode we want to use in this download-box example should contain the following:

  • Url to the file for download
  • Display name
  • Version of the file

And it will look like this when we type it in our blog post

[ download file="myfile.zip" name="My Cool File" version="2.1"]

In order for Wordpress to understand what to do with this shortcode we need to create a plugin. Otherwise it will just be shown as we wrote it in plain text.

1. Creating the plugin

Follow the instructions on Wordpress on how to create a plugin to get started.

First we create the initiator for the plugin.

function SZVisualDownload_init() {

}
add_action('plugins_loaded', 'SZVisualDownload_init');

Then we add our function that will parse the shortcode. And we tell Wordpress to add our shortcode “download” to the system.

function SZVisualDownload_init() {

function download_function($atts) {

}
add_shortcode('download', 'download_function');
}
add_action('plugins_loaded', 'SZVisualDownload_init');

Now we need to write the code to handle the shortcode parameters. This is explained in detailed on the Wordpress Codex page. So you might want to check that out before you continue reading.

Now that you feel confident on how shortcode is parsed we will continue with the actual code to present the beautiful download box.

function download_function($atts) {
extract(shortcode_atts(array(
'file' => '',
'name' => 'No Name',
'version' => '1.0',
), $atts));
$fileurl = get_option('home').'/wp-content/uploads/projects/'.$file;
$filepath = get_option('upload_path').'/projects/'.$file;
$size = byteConvert(filesize($filepath));
return '
<div class="download-area">
<a class="download" href="'.$fileurl.'"><img src="'.get_bloginfo('template_url').'/images/download-button.gif" alt="Download '.$name.' here" /></a>
<h3><a href="'.$fileurl.'">Download '.$name.' v'.$version.'</a></h3>
File size: '.$size.'</div>
';
}

In order to have the file size display better we can add this function and use it on our $size variable.

function byteConvert( $bytes ) {
if ($bytes<=0)
return '0 Byte';

$convention=1000; //[1000->10^x|1024->2^x]
$s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
$e=floor(log($bytes,$convention));
return round($bytes/pow($convention,$e),2).' '.$s[$e];
}

2. Adding some style

Now we have the complete html for the download box. All we need now is to add some CSS to give it the correct style. This CSS inherits from my own theme stylesheet so you will need to adjust it for your own blog. The same goes of course for the images.

div.download-area {
background-image:url(../images/download-bg.gif);
background-repeat:no-repeat;
height:79px;
margin-bottom:15px;
margin-top:15px;
padding:20px;
width:582px;
}
div.download-area img {
float:left;
margin-right:10px;
}

3. Finished!

Now you’re done and you can activate the plugin and try it out. If you want you can download the complete plugin source from the beautiful download box below.

Did you like this article?

  • Simple, neat and nice. Well done Andreas!
  • Yups, and Thanks
  • codee47
    Hy!

    Nice post.

    Download link does not work.
  • The download should be fixed now. Somehow the zip files I create on my mac doesn't work :/
  • Mange tak or takker(I know it's Danish, but I don't know Swedish :-) )
    Is good information. You have a nice site

    Best regards
  • This plugin is nice, assuming that all the downloads are in one location. Since the php file sets the download folder, you have to use that folder.

    I have a few different locations for files that are not in the "uploads" folder...some are and some aren't.

    How would I set the php file to make provision for different locations of files? If I move them all to one location, then I have to change the links to all, right?
  • Correct. This plugin is stupid in the meaning that it does not know or cares what you do with your files. If you move your files you'd have to change all pages where you use the short hand.
  • I've implemented your plugin and modified the images to fit my site. I have to tell you, I am very grateful for your work on this, no matter how easy/difficult or how much/little time it took you.

    Thanks Andreas!

    Wanna see what it looks like?

    http://pclove.us/?p=182
  • One more question for you Andreas...

    How can I add "File type:" below the "File size" call on line 28? I've searched for the parameter, but I'm stabbing in the dark.

    Thanks.
  • In order to add "file type" you'll need to extract that information from the file. You could google the source code for that quite easily.
  • Is there a way to hide the real url???
  • Fred
    Nice. Where's the "beautiful download box?"
blog comments powered by Disqus