Created
January 15, 2014 20:28
-
-
Save zerosignalproductions/8443902 to your computer and use it in GitHub Desktop.
Overhauled the insert image + captions in Wordpress to product html5 output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function custom_insert_image($html, $id, $caption, $title, $align, $url, $size, $alt ) { | |
//Always return an image with a <figure> tag, regardless of link or caption | |
//Grab the image tag | |
$image_tag = get_image_tag($id, '', $title, 'None', $size); | |
//Let's see if this contains a link | |
$linkptrn = "/<a[^>]*>/"; | |
$found = preg_match($linkptrn, $html, $a_elem); | |
// If no link, do nothing | |
if($found > 0) { | |
$a_elem = $a_elem[0]; | |
if(strstr($a_elem, "class=\"") !== false){ // If link already has class defined inject it to attribute | |
$a_elem = str_replace("class=\"", "class=\"colorbox ", $a_elem); | |
} else { // If no class defined, just add class attribute | |
$a_elem = str_replace("<a ", "<a class=\"colorbox\" ", $a_elem); | |
} | |
} else { | |
$a_elem = ""; | |
} | |
// Set up the attributes for the caption <figure> | |
$attributes = (!empty($id) ? ' id="attachment_' . esc_attr($id) . '"' : '' ); | |
$attributes .= ' class="thumbnail wp-caption ' . 'align'.esc_attr($align) . '"'; | |
if ($caption) { | |
$output = '[captionC caption="'.$caption.'" align="'.$align.'"]<figure' . $attributes .'>'; | |
} else { | |
$output = '<figure' . $attributes .'>'; | |
} | |
//add the image back in | |
$output .= $a_elem; | |
$output .= $image_tag; | |
if($a_elem != "") { | |
$output .= '</a>'; | |
} | |
if ($caption) { | |
$output .= '</figure>[/captionC]'; | |
} else { | |
$output .= '</figure>'; | |
} | |
return $output; | |
} | |
function custom_caption_shortcode($attr, $content = null) | |
{ | |
extract(shortcode_atts(array( | |
'id' => '', | |
'align' => '', | |
'caption' => '' | |
), $attr)); | |
return str_replace ( '</figure>' , '<figcaption class="caption wp-caption-text">'.$caption.'</figcaption></figure>' , $content ); | |
} | |
add_filter('image_send_to_editor', 'custom_insert_image', 10, 9); | |
add_filter('disable_captions', create_function('$a', 'return true;')); | |
add_shortcode( 'captionC', 'custom_caption_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment