Skip to content

Instantly share code, notes, and snippets.

@ffoodd
Forked from JoostKiens/functions.php
Last active December 24, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffoodd/6740308 to your computer and use it in GitHub Desktop.
Save ffoodd/6740308 to your computer and use it in GitHub Desktop.
<?php
/**
* @note : Caption content is filtered to use html5 semantic elements figure & figcaption. First found on Zhen Huang's "Reverie".
* @author : @milohuang
* @see : http://themefortress.com/reverie/
* @see : https://github.com/milohuang/reverie/blob/master/lib/clean.php
* @note : Then we add microdata. Found on Joost Kiens' Gists.
* @author : @joostkiens
* @see : https://gist.github.com/JoostKiens/4477366
* @note : And in the end I add all ARIA role and attributes we need (group, aria-labelledby, aria-describedby). Group role is due to a lack of support of figure semantic.
* @see : http://www.kloh.ch/craw2013-html5-aria-et-accessibilite-web-479
*
* @param string $val Empty
* @param array $attr Shortcode attributes
* @param string $content Shortcode content
* @return string Shortcode output
*/
function ffeeeedd__caption( $output, $attr, $content ) {
if ( is_feed() ) {
return $output;
}
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
$attr = shortcode_atts( $defaults, $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return $content;
}
$content = str_replace( '<img', '<img id="' . $attr['id'] . '" itemprop="contentURL" aria-labelledby="wp-caption_' . $attr['id'] . '"', $content );
$attributes = ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"';
$output = '<figure' . $attributes .' role="group" itemscope itemtype="http://schema.org/ImageObject">';
$output .= do_shortcode( $content );
$output .= '<figcaption class="wp-caption-text pt1 " id="wp-caption_' . $attr['id'] . '" aria-describedby="' . $attr['id'] . '" itemprop="description">' . $attr['caption'] . '</figcaption>';
$output .= '</figure>';
return $output;
}
add_filter( 'img_caption_shortcode', 'ffeeeedd__caption', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment