Automate File Attachments on your WordPress Posts
Saturday, January 16th, 2010UPDATE 4/24/2010: I updated the audio section to automatically wrap the audio with the flash player based on the example provided by Joseph Hinson
I’ve been coding WordPress sites for 4 or 5 years now. The more I use it, the more I love it. WordPress is an awesome CMS tool. When I create a site for a client, I want the site to be as easy as possible for a “non” web designer/developer types to maintain. One of my latest clients wanted to be able to create a post and attach a file for download. The client found it a little confusing to attach the file to a post and then include that file as a link. They also wanted to show an icon specific to the document type they had uploaded. With a little code, I was able to automate the process of linking to files attached to a WordPress post and specify the icon based on the files MIME type. There are plenty of articles on the web about WordPress attachments so I’m not going into any detail about that. Here is an article on attachments by Jeff Starr I’ve bookmarked for reference. Digging into WordPress
Here is an example of what we are about to create. I’ve attached several sample files to this post and that’s it. By the way, the zip file listed below is a zip of all the icons for you to download.
First let’s create the function and the shortcode. Go to your themes folder and open functions.php. If you don’t have one, just create a new file named functions.php and save it to your themes folder.
Create a new function with the following code.
function get_attachment_icons($echo = false){
//PDF Documents
if ( $files = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/pdf', //MIME Type condition
))){
foreach( $files as $file ){ //setup array for more than one file attachment
$file_link = wp_get_attachment_url($file->ID); //get the url for linkage
$file_name_array=explode("/",$file_link);
$file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename
$sAttachmentString .= "<div class='documentIcons'>";
$sAttachmentString .= "<a href='$file_link'>";
$sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/mime/pdf.png'/>";
$sAttachmentString .= "</a>";
$sAttachmentString .= "<br>";
$sAttachmentString .= "<a href='$file_link'>$file_name[0]</a>";
$sAttachmentString .= "</div>";
}
}
This block is for PDF files. You can tell that by the line that says ‘post_mime_type’ => ‘application/pdf’,. Just change the MIME type for the type of file you are expecting. In the final code, I’ve added a new loop for each MIME type below:
- PDF = application/pdf
- Word Documents = application/msword
- PowerPoint = application/vnd.ms-powerpoint
- Excel = application/vnd.ms-excel
- Zip = application/zip
- Audio files = audio/mpeg
Now at the bottom of the function we will add the hook for the shortcode and we’re done!
add_shortcode('attachment icons', 'get_attachment_icons');
Usage
Just include a shortcode in any post
[attachment icons]
or place one line in your single.php file (or where ever you’d like the document icons to show up)
<?php get_attachment_icons($echo=true); ?>
Final Code
Here is the code altogether for your copying and pasting pleasure. Enjoy!
function get_attachment_icons($echo = false){
$sAttachmentString = "<div class='documentIconsWrapper'> \n";
if ( $files = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/pdf', //MIME Type condition
))){
foreach( $files as $file ){ //setup array for more than one file attachment
$file_link = wp_get_attachment_url($file->ID); //get the url for linkage
$file_name_array=explode("/",$file_link);
$file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename
$sAttachmentString .= "<div class='documentIcons'>";
$sAttachmentString .= "<a href='$file_link'>";
$sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/mime/pdf.png'/>";
$sAttachmentString .= "</a>";
$sAttachmentString .= "<br>";
$sAttachmentString .= "<a href='$file_link'>$file_name[0]</a>";
$sAttachmentString .= "</div>";
}
}
//Word Documents
if ( $files = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/msword', //MIME Type condition
))){
foreach( $files as $file ){ //setup array for more than one file attachment
$file_link = wp_get_attachment_url($file->ID); //get the url for linkage
$file_name_array=explode("/",$file_link);
$file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename
$sAttachmentString .= "<div class='documentIcons'>";
$sAttachmentString .= "<a href='$file_link'>";
$sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/mime/word.png'/>";
$sAttachmentString .= "</a>";
$sAttachmentString .= "<br>";
$sAttachmentString .= "<a href='$file_link'>$file_name[0]</a>";
$sAttachmentString .= "</div>";
}
}
//Powerpoint Documents
if ( $files = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/vnd.ms-powerpoint', //MIME Type condition
))){
foreach( $files as $file ){ //setup array for more than one file attachment
$file_link = wp_get_attachment_url($file->ID); //get the url for linkage
$file_name_array=explode("/",$file_link);
$file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename
$sAttachmentString .= "<div class='documentIcons'>";
$sAttachmentString .= "<a href='$file_link'>";
$sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/mime/PowerPoint.png'/>";
$sAttachmentString .= "</a>";
$sAttachmentString .= "<br>";
$sAttachmentString .= "<a href='$file_link'>$file_name[0]</a>";
$sAttachmentString .= "</div>";
}
}
//Excel Documents
if ( $files = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/vnd.ms-excel', //MIME Type condition
))){
foreach( $files as $file ){ //setup array for more than one file attachment
$file_link = wp_get_attachment_url($file->ID); //get the url for linkage
$file_name_array=explode("/",$file_link);
$file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename
$sAttachmentString .= "<div class='documentIcons'>";
$sAttachmentString .= "<a href='$file_link'>";
$sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/mime/XLS8.png'/>";
$sAttachmentString .= "</a>";
$sAttachmentString .= "<br>";
$sAttachmentString .= "<a href='$file_link'>$file_name[0]</a>";
$sAttachmentString .= "</div>";
}
}
//Zipped Files
if ( $files = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/zip', //MIME Type condition
))){
foreach( $files as $file ){ //setup array for more than one file attachment
$file_link = wp_get_attachment_url($file->ID); //get the url for linkage
$file_name_array=explode("/",$file_link);
$file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename
$sAttachmentString .= "<div class='documentIcons'>";
$sAttachmentString .= "<a href='$file_link'>";
$sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/mime/zip.png'/>";
$sAttachmentString .= "</a>";
$sAttachmentString .= "<br>";
$sAttachmentString .= "<a href='$file_link'>$file_name[0]</a>";
$sAttachmentString .= "</div>";
}
}
//Audio Files
$mp3s = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'audio', //MIME Type condition
) );
if (!empty($mp3s)) :
$sAttachmentString .= "<ul class='audiofiles'>";
foreach($mp3s as $mp3) :
$sAttachmentString .= "<li>";
if(!empty($mp3->post_title)) : //checking to make sure the post title isn't empty
$sAttachmentString .= "<h4 class='title'>".$mp3->post_title."</h4>";
endif;
if(!empty($mp3->post_content)) : //checking to make sure something exists in post_content (description)
$sAttachmentString .= "<p class='description'>".$mp3->post_content."</p>";
endif;
$sAttachmentString .= "<object width='470' height='24' id='single".$mp3->ID."' name='single".$mp3->ID."'>";
$sAttachmentString .= "<param name='movie' value='player.swf'>";
$sAttachmentString .= "<param name='allowfullscreen' value='true'>";
$sAttachmentString .= "<param name='allowscriptaccess' value='always'>";
$sAttachmentString .= "<param name='wmode' value='transparent'>";
$sAttachmentString .= "<param name='flashvars' value='file=".$mp3->guid."'>";
$sAttachmentString .= "<embed ";
$sAttachmentString .= "id='single".$mp3->ID."' ";
$sAttachmentString .= "name='single".$mp3->ID."' ";
$sAttachmentString .= "src='".get_bloginfo('template_directory')."/jw/player.swf' ";
$sAttachmentString .= "width='470' ";
$sAttachmentString .= "height='24' ";
$sAttachmentString .= "bgcolor='#ffffff' ";
$sAttachmentString .= "allowscriptaccess='always' ";
$sAttachmentString .= "allowfullscreen='true' ";
$sAttachmentString .= "flashvars='file=".$mp3->guid."' ";
$sAttachmentString .= "/>";
$sAttachmentString .= "</object>";
$sAttachmentString .= "<a href='".$mp3->guid."'>Download</a>";
$sAttachmentString .= "</li>";
endforeach;
$sAttachmentString .= "</ul>";
endif;
$sAttachmentString .= "</div>";
if($echo){
echo $sAttachmentString;
}
return $sAttachmentString;
}
add_shortcode('attachment icons', 'get_attachment_icons');















Are you sick of clicking on the “Folders” button to see the folder tree everytime you open Windows Explorer? Here are the simple steps to make that folder tree show by default.



















