Skip to content

Instantly share code, notes, and snippets.

@tomazzaman
Created February 18, 2015 23:57
Show Gist options
  • Save tomazzaman/b952b3f23dfdbf499e79 to your computer and use it in GitHub Desktop.
Save tomazzaman/b952b3f23dfdbf499e79 to your computer and use it in GitHub Desktop.
Insert image in WordPress with HTML5 <figure> tag and caption
<?php
// Don't include the opening tag
function html5_insert_image( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
$src = wp_get_attachment_image_src( $id, $size, false );
$html5 = "<figure id=\"post-$id media-$id\" class=\"align-$align\">";
if ( $url ) {
$html5 .= "<a href=\"$url\" class=\"image-link\"><img src=\"$src[0]\" alt=\"$alt\" /></a>";
} else {
$html5 .= "<img src=\"$src[0]\" alt=\"$alt\" />";
}
if ( $caption ) {
$html5 .= "<figcaption>$caption</figcaption>";
}
$html5 .= "</figure>";
return $html5;
}
add_filter( 'image_send_to_editor', 'html5_insert_image', 10, 9 );
@carasmo
Copy link

carasmo commented Jan 21, 2018

Adds two figure tags when there is a caption.

@dameer
Copy link

dameer commented Nov 27, 2018

No good here at all! If you try to remove image from editor FIGURE tags are not deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment