Skip to content

Instantly share code, notes, and snippets.

@rezen
Created August 18, 2012 17:05
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rezen/3388415 to your computer and use it in GitHub Desktop.
Save rezen/3388415 to your computer and use it in GitHub Desktop.
Filter Image insert for WordPress
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 );
@chriscoyier
Copy link

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!

@rezen
Copy link
Author

rezen commented Aug 23, 2012

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.

@natejacobson
Copy link

Hattip, @rezen. Thanks.

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