如何阻止WordPress在带有标题的图像附件中注入硬编码样式

时间:2017-08-24 作者:jonbon

每次我向wordpress图像添加标题时,wordpress都会决定在附件html中插入硬编码样式=“”,这会破坏我的侧边栏。

enter image description here

所有没有标题的图像都会被插入,完全正常地像这样(这不会破坏主题)

enter image description here

1 个回复
SO网友:birgire

我想你在找img_caption_shortcode_width 过滤器,在img_caption_shortcode():

/**
 * Filters the width of an image\'s caption.
 *
 * By default, the caption is 10 pixels greater than the width of the image,
 * to prevent post content from running up against a floated image.
 *
 * @since 3.7.0
 *
 * @see img_caption_shortcode()
 *
 * @param int    $width    Width of the caption in pixels. To remove this inline style,
 *                         return zero.
 * @param array  $atts     Attributes of the caption shortcode.
 * @param string $content  The image element, possibly wrapped in a hyperlink.
 */
$caption_width = apply_filters( \'img_caption_shortcode_width\', $width, $atts, $content );
所以看起来应该禁用内联样式:

add_filter( \'img_caption_shortcode_width\', \'__return_zero\' );

结束