这将执行以下操作:
<?php
/*
* This filter only works with images, for all kind of media check: media_send_to_editor
* The priority is set to 20 and it takes 8 arguments
*/
add_filter(\'image_send_to_editor\', \'wpse_53577_img_wrapper\', 20, 8);
// We are only working with the $html argument, but you can play with all of them
function wpse_53577_img_wrapper($html, $id, $caption, $title, $align, $url, $size, $alt)
{
// If Link URL is set to "File URL" or "Attachment Post URL", the anchor tag gets replaced with the div tag
$new_html = preg_replace("/\\<a(.*)\\>(.*)\\<\\/a\\>/iU", "<div class=\\"image-container\\">$2</div>", $html);
// If no replacement was done (Link URL == None), wrap the image tag with the div tag
$html = ($new_html == $html) ? \'<div class="image-container">\'. $html . \'</div>\' : $new_html;
return $html;
}
Reference snapshot:
锚标记的替换是用正则表达式(RegEx)完成的,我不熟悉正则表达式。。。发现它在做this search.