How to implement automatic HTML5 Figure Captions for WordPress
Since WordPress is updated to 3.4, many people encounter problems with their figure captions in HTML5 themes.
Written in 2012. Some details, links or tools may be outdated, but I've kept it here for the record.
Since WordPress is updated to 3.4, many people encounter problems with their figure captions in HTML5 themes. HTML5 themes are nice, but many still use the old way of implementing figure captions. There is however a solution which is fairly easy, and this will be explained in this article.
Now the reason that I share it here is that I first had to delve through some WordPress Support Forum posts, Stackoverflow and websites promoting the wrong solution before finding the solution somewhere hidden in the haystack. Hence, I give props to Andidas for implementing figure captions the right way.
The broken WordPress figcaption solution:
Now, many people and themes used the ‘add_shortcode’ approach, for example here. Basically, it inserts some code to the existing WordPress image caption shortcode like below:
add_shortcode('wp_caption', 'custom_img_caption_shortcode_function');
add_shortcode('caption', 'custom_img_caption_shortcode_function');
This replaces the existing caption shortcodes with a custom written build, but it is better to filter the existing function for displaying the shortcode.
Update: core support for HTML5 figcaption
Since WordPress 3.6, WordPress Core supports HTML 5 captions. This makes it bloody easy to enable HTML5 figure captions automatically. It can be done in the following way:
add_theme_support( 'html5', array( 'caption' ) );
Place this anywhere in your themes functions location, most likely functions.php to have effect.
The original code for allowing HTML5 figure captions
Before the support for html5 caption was added to core, the best approach was to hook upon the caption_shortcode filter. Hence, a better approach is to add a filter to the default caption_shortcode hook that is generating the captions and thus override it with the function of the filter. Thus, a better solution would be this (built upon Andidas his approach):
/**
* Filters the default caption shortcode filter to allow HTML5 captions in Wordpress
*
* Please note that since Wordpress 3.6 you can easily add HTML5 captions via add_theme_support
* using the following line of code:
* add_theme_support( 'html5', array( 'caption' ) );
* More information: https://codex.wordpress.org/Function_Reference/add_theme_support#HTML5
*
* @param string $value Empty string, passed if no caption is set
* @param array $attr The array of shortcode attributes
* @param string $content The content within the shortcode
*/
function caption_shortcode_filter($value, $attr, $content = null) {
extract(
shortcode_atts(array(
'id' => '',
'align' => '',
'width' => '',
'caption' => ''
), $attr)
);
// If the caption is empty.
if ( 1 > (int) $width || empty($caption) ) {
return $value;
}
// Get the image id and add it to the caption for specification
if ( $id ) {
$id = esc_attr($id);
$capid = 'id="figure_caption_'. $id . '" ';
$id = 'id="' . $id . '" aria-labelledby="figure_caption_' . $id . '" ';
}
// Return the figure element with the proper width, the image and the figure caption
$output = '<figure ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: '
. ((int) $width) . 'px">' . do_shortcode( $content ) . '<figcaption ' . $capid
. 'class="wp-caption-text">' . $caption . '</figcaption></figure>';
return $output;
}
add_filter('img_caption_shortcode', 'caption_shortcode_filter',10,3);
Using this code, your captions will automatically be a HTML5 figure caption. This code should most likely be placed in your themes functions.php or any custom function script.