Shoora
11/26/2018 - 1:39 AM

Prevent Wordpress from inserting an extra 10px of width to image caption shortcodes.

Prevent Wordpress from inserting an extra 10px of width to image caption shortcodes.

<?php
// DO NOT INCLUDE OPENING PHP TAG

add_filter( 'img_caption_shortcode', 'custom_remove_additional_10px_from_captions', 10, 3 );
/**
 * Prevent Wordpress from inserting an extra 10px of width to image caption shortcodes.
 */
function custom_remove_additional_10px_from_captions( $val, $attr, $content = null ) {

    extract( shortcode_atts( array(
        'id'      => '',
        'align'   => '',
        'width'   => '',
        'caption' => ''
    ), $attr ) );

    if ( 1 > ( int ) $width || empty( $caption ) ) return $val;

    return '<div id="' . $id . '" class="wp-caption ' . esc_attr( $align ) . '">' . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}