Skip to content

Instantly share code, notes, and snippets.

@beeriz
Forked from johndugan/gist:4359828
Last active April 6, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beeriz/e194e40d47a90a0720c8cfd599256750 to your computer and use it in GitHub Desktop.
Save beeriz/e194e40d47a90a0720c8cfd599256750 to your computer and use it in GitHub Desktop.
WordPress: remove inline width from figure
<?php
add_shortcode('wp_caption', 'fixed_img_caption_shortcode');
add_shortcode('caption', 'fixed_img_caption_shortcode');
function fixed_img_caption_shortcode($attr, $content = null) {
if ( ! isset( $attr['caption'] ) ) {
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
$content = $matches[1];
$attr['caption'] = trim( $matches[2] );
}
}
$output = apply_filters('img_caption_shortcode', '', $attr, $content);
if ( $output != '' )
return $output;
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<figure ' . $id . ' ' . esc_attr($align) . '" style="width: auto">'
. do_shortcode( $content ) . '<figcaption class="-text">' . $caption . '</figcaption></figure>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment