# WordPress如何自动从插入的图像中删除宽度和高度属性

*Published:* 2023-10-27
*Author:* 客服001

#### 温馨提醒

如果文章内容或图片资源失效，请留言反馈，我们会及时处理，谢谢

本文最后更新于2023年10月27日，已超过 180天没有更新



当通过 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;
}
```