Change inserted HTML for Wordpress images Change inserted HTML for Wordpress images wordpress wordpress

Change inserted HTML for Wordpress images


This is the correct filter you should use:

/** * Filters the image HTML markup including the caption shortcode. * * @since 2.6.0 * * @param string $shcode The image HTML markup with caption shortcode. * @param string $html   The image HTML markup. */return apply_filters( 'image_add_caption_shortcode', $shcode, $html );

For example:

function my_custom_markup( $shcode, $html ) {    return str_replace( "caption", "something_new" , $shcode );}add_filter( 'image_add_caption_shortcode','my_custom_markup', 10, 2 );


You can use the following filter:

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );function my_img_caption_shortcode( $empty, $attr, $content ){    $attr = shortcode_atts( array(        'id'      => '',        'align'   => 'alignnone',        'width'   => '',        'caption' => ''    ), $attr );    if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {        return '';    }    if ( $attr['id'] ) {        $attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';    }    return '<figure ' . $attr['id']    . 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '    . 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'    . do_shortcode( $content )    . '<figcaption  class="figure-caption">' . $attr['caption'] . '</figcaption>'    . '</figure>';}

Source: Plugin API/Filter Reference/img caption shortcode « WordPress Codex