Skip to content

Instantly share code, notes, and snippets.

@georgiecel
Last active August 29, 2015 14:23
Show Gist options
  • Save georgiecel/6ab43b153427a914cd25 to your computer and use it in GitHub Desktop.
Save georgiecel/6ab43b153427a914cd25 to your computer and use it in GitHub Desktop.
semantic figure and figcaption elements for captioned images in WordPress – insert into functions.php
function caption_shortcode($val, $attr, $content = null) {
extract(shortcode_atts(array('id'=> '','align'=> 'aligncenter','width'=> '','caption' => ''), $attr));
if ( 1 > (int) $width || empty($caption) )return $val;
$capid = '';
if ( $id ) {
$id = esc_attr($id);
$capid = 'id="figcaption_'. $id . '" ';
$id = 'id="' . $id . '" aria-labelledby="figcaption_' . $id . '" ';
}
return '<figure ' . $id . 'class="post-figure figure-' . esc_attr($align) . '" style="width: '. (int) $width . 'px">'
. do_shortcode( $content ) . '<figcaption ' . $capid . 'class="post-figcaption">' . $caption . '</figcaption></figure>';
}
add_filter('img_caption_shortcode', 'caption_shortcode', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment