Skip to content

Instantly share code, notes, and snippets.

@SimonFricker
Forked from JoostKiens/functions.php
Created October 11, 2016 20:30
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 SimonFricker/cb93fe90d9633bb342edc84f2c2640dc to your computer and use it in GitHub Desktop.
Save SimonFricker/cb93fe90d9633bb342edc84f2c2640dc to your computer and use it in GitHub Desktop.
Improve the WordPress caption shortcode with HTML5 figure & figcaption, microdata & wai-aria attributes
<?php
/**
* Improves the WordPress caption shortcode with HTML5 figure & figcaption, microdata & wai-aria attributes
*
* Author: @joostkiens
* Licensed under the MIT license
*
* @param string $val Empty
* @param array $attr Shortcode attributes
* @param string $content Shortcode content
* @return string Shortcode output
*/
function my_img_caption_shortcode_filter( $val, $attr, $content = null ) {
extract( shortcode_atts( array(
'id' => '',
'align' => 'aligncenter',
'width' => '',
'caption' => ''
), $attr ) );
// No caption, no dice...
if ( 1 > (int) $width || empty( $caption ) )
return $val;
if ( $id )
$id = esc_attr( $id );
// Add itemprop="contentURL" to image - Ugly hack
$content = str_replace('<img', '<img itemprop="contentURL"', $content);
return '<figure id="' . $id . '" aria-describedby="figcaption_' . $id . '" class="wp-caption ' . esc_attr($align) . '" itemscope itemtype="http://schema.org/ImageObject" style="width: ' . (0 + (int) $width) . 'px">' . do_shortcode( $content ) . '<figcaption id="figcaption_'. $id . '" class="wp-caption-text" itemprop="description">' . $caption . '</figcaption></figure>';
}
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode_filter', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment