Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Created January 15, 2013 04:13
Show Gist options
  • Save BronsonQuick/4535974 to your computer and use it in GitHub Desktop.
Save BronsonQuick/4535974 to your computer and use it in GitHub Desktop.
Remove extra 10px that WordPress adds to captions props to Justin Tadlock: http://justintadlock.com/archives/2011/07/01/captions-in-wordpress
<?php
/**
* This function removes the extra 10px width that WordPress adds to captions
*
* @param $output
* @param $attr
* @param $content
*
* @return string
*/
function cleaner_caption( $output, $attr, $content ) {
/* We're not worried abut captions in feeds, so just return the output here. */
if ( is_feed() )
return $output;
/* Set up the default arguments. */
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
/* Merge the defaults with user input. */
$attr = shortcode_atts( $defaults, $attr );
/* If the width is less than 1 or there is no caption, return the content wrapped between the [caption]< tags. */
if ( 1 > $attr['width'] || empty( $attr['caption'] ) )
return $content;
/* Set up the attributes for the caption <div>. */
$attributes = ( !empty( $attr['id'] ) ? ' id="' . esc_attr( $attr['id'] ) . '"' : '' );
$attributes .= ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"';
$attributes .= ' style="width: ' . esc_attr( $attr['width'] ) . 'px"';
/* Open the caption <div>. */
$output = '<div' . $attributes .'>';
/* Allow shortcodes for the content the caption was created for. */
$output .= do_shortcode( $content );
/* Append the caption text. */
$output .= '<p class="wp-caption-text">' . $attr['caption'] . '</p>';
/* Close the caption </div>. */
$output .= '</div>';
/* Return the formatted, clean caption. */
return $output;
}
add_filter( 'img_caption_shortcode', 'cleaner_caption', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment