Skip to content

Instantly share code, notes, and snippets.

@ideag
Last active October 14, 2021 17:50
Show Gist options
  • Save ideag/5055492 to your computer and use it in GitHub Desktop.
Save ideag/5055492 to your computer and use it in GitHub Desktop.
add_filter( 'image_send_to_editor', 'tiny_force_caption', 100 );
function tiny_force_caption( $html ) {
$a = strpos($html, 'caption');
if ($a!=1) {
preg_match('/(alignnone|alignleft|alignright|aligncenter)/', $html,$c);
preg_match('/width="(\d*)"/', $html,$w);
preg_match('/alt="([^"]*)"/', $html,$m);
preg_match('/wp-image-(\d*)"/', $html,$n);
if (!isset($n[1])) $n[1] = '0';
$html = '[caption id="attachment_'.$n[1].'" align="'.($c?$c[1]:'alignnone').'" width="'.$w[1].'"]'.$html." © ".get_bloginfo('title')."[/caption]";
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment