Insert image in WordPress with HTML5 tag and caption function

The width and height are [1] and [2]. So these are added on the img tag. If a theme has html5 caption support this outputs mangled html if you have a caption (it’s pretty standard these days).

Corrected to check if there is a caption and return the WP shortcode or if no caption, then use the figure markup. Tested and works.

function html5_insert_image( $html, $id, $caption, $title, $align, $url, $size, $alt ) {

    if( empty( $caption ) ) :

        $src  = wp_get_attachment_image_src( $id, $size, false );

        $html = "<figure id=\"post-$id media-$id\" class=\"align-$align\">";

        if ( $url ) {
            $html .= "<a href=\"$url\" class=\"image-link\"><img src=\"$src[0]\" width=\"$src[1]\" height=\"$src[2]\" alt=\"$alt\" /></a>";
        } else {
            $html .= "<img src=\"$src[0]\" width=\"$src[1]\" height=\"$src[2]\" alt=\"$alt\" />";
        }

        $html .= "</figure>";

    endif;

    return $html;

}
add_filter( 'image_send_to_editor', 'html5_insert_image', 10, 9 );