A nice cool way to organize the attachments from a post or page, and not having to deal with ugly html in the wysiwyg editor, is using shortcodes. Just add the code below into your theme functions.php.
// [attachments] function attachmentUrl($atts) { extract(shortcode_atts(array( "src" => '' ), $atts)); /* $filename_match = preg_match("/([a-zA-Z0-9_\.\-]+\.[a-zA-Z]+)$/", $src, $filename); */ $filename_match = preg_match("/\/([^\/]*$)/", $src, $filename); return '<div class="attachments"><h4>Attachments</h4><ul class="attachments_list attachment"><li><a href="'.$src.'" target="_blank">'.$filename[1].'</a></li></ul></div>'; } add_shortcode("attachment", "attachmentUrl"); // Audio shortcode function audioURL($atts) { extract(shortcode_atts(array( "src" => '' ), $atts)); return '<audio src="'.$src.'" type="audio/mp3" controls="controls"></audio>'; } add_shortcode("audio", "audioURL"); // Video shortcode function videoURL($atts) { extract(shortcode_atts(array( "src" => '' ), $atts)); return '<video controls="controls" preload="none"><source src="'.$src.'" type="video/mp4" /></video>'; } add_shortcode("video", "videoURL"); // Customize the 'insert into Post' action add_filter('media_send_to_editor', 'my_filter_iste', 20, 3); function my_filter_iste($html, $id, $caption, $title, $align, $url, $size, $alt) { $attachment = get_post($id); //fetching attachment by $id passed through $src = wp_get_attachment_url( $id ); $videoext = '.mp4'; $audioext = '.mp3'; $mime_type = $attachment->post_mime_type; //getting the mime-type if ((substr($mime_type, 0, 5) == 'video') || (substr_compare($src, $videoext, strlen($str)-strlen($videoext), strlen($videoext)) === 0)) { //checking mime-type $html = '[video src="'.$src.'"]'; } elseif ((substr($mime_type, 0, 5) == 'audio') || (substr_compare($src, $audioext, strlen($str)-strlen($audioext), strlen($audioext)) === 0)) { //checking mime-type $html = '[audio src="'.$src.'"]'; } else { $html = '[attachment src="'.$src.'"]'; } return $html; // return new $html }
and the ‘insert into post’ action will insert a shortcode similar to [attachment src=”URL_to_attachment”] , that returns in frontend the HTML that corresponds to the uploaded file mime type.
Leave a Reply