Posts Tagged ‘WordPress’

iPhone 4 sweetness! :D #iphone…

Tuesday, July 20th, 2010

iPhone 4 sweetness! :D #iphone4

Nothing wimpy but the name htt…

Monday, July 5th, 2010

Nothing wimpy but the name http://yfrog.com/7hliuoj

@jtoddmason just exercise more…

Friday, June 25th, 2010

@jtoddmason just exercise more!

French Fries are not a vegetab…

Monday, April 26th, 2010

French Fries are not a vegetable! http://tinyurl.com/23jfe47

Drag and Drop Gmail Attachment…

Thursday, April 15th, 2010

Drag and Drop Gmail Attachments http://bit.ly/a235g7

He is risen!

Sunday, April 4th, 2010

He is risen!

Touch my egg and I’ll bite you…

Sunday, April 4th, 2010

Touch my egg and I’ll bite you. I’ve had a rough day! http://tinyurl.com/y9hmanl

Sunday’s coming!!

Friday, April 2nd, 2010

Sunday’s coming!!

COMBO! http://tinyurl.com/y9cy…

Monday, March 29th, 2010

COMBO! http://tinyurl.com/y9cy9ep

I got the Mac Heist bundle. 7 …

Friday, March 5th, 2010

I got the Mac Heist bundle. 7 fantastic Mac apps worth $260+ for only $19.95 and got 3 sweet bonus apps free! http://bit.ly/heist-it

what’s all the buzz about?

Wednesday, February 10th, 2010

what’s all the buzz about?

Automate File Attachments on y…

Saturday, January 16th, 2010

Automate File Attachments on your WordPress Posts http://bit.ly/5A7HFH #wordpress

Automate File Attachments on your WordPress Posts

Saturday, January 16th, 2010

UPDATE 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.

  • Jeopardy Think Music

    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');

    Deny Access to No Referrer Requests

    Wednesday, October 31st, 2007

    When your readers comment, the wp-comments-post.php file is accessed, does its thing, and creates the post. The user’s browser will send a “referral” line about this.

    When a spam-bot comes in, it hits the file directly and usually does not leave a referrer. This allows for some nifty detection and action direct from the server. If you are not familiar with Apache directives, then write the following in your root directory .htaccess file::

    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} POST
    RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
    RewriteCond %{HTTP_REFERER} !.*yourdomain.com.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^$
    RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

    This will:

    Detect when a POST is being made
    Check to see if the post is on wp-comments-post.php
    Check if the referrer is in your domain or if no referrer
    Send the spam-bot BACK to its originating server’s IP address.
    NOTE 1: In the 4th line, change yourdomain.com to your domain.xxx without the www or any prefix for that matter.
    NOTE 2: There is a slim chance that someone’s browser will not send the referral, but this is extremely rare.

    This essentially deflects the spam-bot back on itself.

    Taken from WordPress Codex

    Christmas by Candlelight

    Wednesday, December 20th, 2006

    [VIDEO=6]Here, for your viewing pleasure :) is a very shaky video of the youth choirs at my church performing this past Sunday evening. My tripod is a piece of junk so I pulled the camera off and… well you see the result. One of these days I’m gonna get me one of them there fancy tripods. Holden is playing the drumset and Lizzy is singing in the first choir. Wendy is singing in the second choir and she hates those robes! Just FYI. I’m using a very cool WordPress plugin by Alex Rabe to manage the video on this site.