当您通过 WordPress 媒体上传器上传图像,然后将其插入编辑器时,它带有宽度和高度属性。这些通常是可取的,因为它有助于浏览器在布局期间为图像腾出适当的空间。但是,如果您想从添加这些属性中删除插入操作,您可以将此代码添加到您的functions.php文件或您自己制作的功能插件中:

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}