在将自定义图像数据插入帖子时,最好使用media_send_to_editor
, 需要注意的是,这不会应用于任何现有图像,只会应用于添加功能后插入的图像。
要将其应用于现有内容,您将使用preg_match
在…上the_content
过滤,但这会显著降低站点速度,建议您不要像这样解析输出。
A.media_send_to_editor
示例:
function WPSE_78285_Mime($html, $id) {
//fetching attachment by post $id
$attachment = get_post($id);
$mime_type = $attachment->post_mime_type;
//get an valid array of images types
$image_exts = array( \'image/jpg\', \'image/jpeg\', \'image/jpe\', \'image/gif\', \'image/png\' );
//checking the above mime-type
if (in_array($mime_type, $image_exts)) {
// the image link would be great
$src = wp_get_attachment_url( $id );
// enter you custom output here, you will want to probably change this
$html = \'<a href="\' . $src . \'" class="your-class" data-src="/img/image1.jpg" rel="your-rel"><img src="\'. $src .\'" /></a>\';
return $html; // return new $html
}
return $html;
}
add_filter(\'media_send_to_editor\', \'WPSE_78285_Mime\', 20, 2);