public
Last active

Filter Image insert for WordPress

  • Download Gist
wp.filter.php
PHP
1 2 3 4 5 6 7 8 9 10 11
function xf_insert_image( $html, $id, $caption, $title, $align, $url )
{
$html5 = "<figure id='post-$id media-$id' class='align-$align'>";
$html5 .= "<img src='$url' alt='$title' />";
$html5 .= "<figcaption>$caption</figcaption>";
$html5 .= "</figure>";
return $html5;
}
add_filter( 'image_send_to_editor', 'xf_insert_image', 10, 9 );

Only change I'd make is not outputting figcaption if no caption:

function html5_insert_image($html, $id, $caption, $title, $align, $url) {
  $html5 = "<figure id='post-$id media-$id' class='align-$align'>";
  $html5 .= "<img src='$url' alt='$title' />";
  if ($caption) {
    $html5 .= "<figcaption>$caption</figcaption>";
  }
  $html5 .= "</figure>";
  return $html5;
}
add_filter( 'image_send_to_editor', 'html5_insert_image', 10, 9 );

Thanks!

True that!

I just wish I could figure out how to modify the output when the user edits the image align and class in the visual editor.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.