将图像插入帖子的那一刻,钩子image_send_to_editor
被调用。钩子的输入是html,也可能是标题短代码。像这样:
[caption id="attachment_999" align="alignright" width="150"]
<a href="http://www.example.com/wp-content/uploads/2016/06/my-image.jpg">
<img src="http://www.example.com/wp-content/uploads/2016/06/my-image-150x150.jpg" alt="My colourful image" width="150" height="150" class="size-thumbnail wp-image-101" />
</a>
Look at how colourful this is!
[/caption]
钩子截取此字符串,允许您操纵它:
add_filter (\'image_send_to_editor\', \'wpse_209128_filter_image\');
function wpse_209128_filter_image ($html) {
// first, remove the image and caption dimensions
$html = preg_replace (\'/(width|height)=\\"\\d*\\"\\s/\', \'\', $html);
// second, replace the class on the img tag
$html = preg_replace (\'/class=\\"([^"]*)\\", \'class="img-responsive"\', $html);
return $html;
}
注意,一旦在帖子中插入了img,用户仍然可以编辑html并把事情搞砸。