My theme has a shortcode that outputs the caption, credits and date of images, but if the image has no caption, even if there is credits and/or date set nothing shows, and the image doesnt enter the function. it gets just wrapped in a

by default. My question is how can i edit the html of this

so i can add the credits/date part to the image if there is no caption defined. This is the code for the one with captions:

    function update_caption_shortcode( $empty, $attr, $content ){


    $atts = shortcode_atts( array(
        'id'      => '',
        'align'   => 'alignnone',
        'width'   => '',
        'caption' => '',
        'class'   => '',
    ), $attr, 'caption' );

    $atts['width'] = (int) $atts['width'];
    if ( $atts['width'] < 1 || empty( $atts['caption'] ) )
        return $content;

    if ( ! empty( $atts['id'] ) )
        $atts['id'] = 'id="' . esc_attr( sanitize_html_class( $atts['id'] ) ) . '" ';

    $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );

    $html5 = current_theme_supports( 'html5', 'caption' );
    // HTML5 captions never added the extra 10px to the image width
    $width = $html5 ? $atts['width'] : ( 10 + $atts['width'] );

    $caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content );

    //Get att id
    $att_id      = str_replace('"', "", $atts['id']);
    $att_id      = explode('_', $att_id);
    $att_id      = intval( $att_id[1] );
    $attachment  = get_post( $att_id );
    if (isset($attachment)) {
        $credits = get_post_meta( $att_id, 'photographer_name', true );
        $date    = get_post_meta( $att_id, 'photo_date', true );
    }

    $style = '';
    if ( $caption_width )
        $style = 'style="width: ' . (int) $caption_width . 'px" ';

    $html = '';

    if ( $html5 ) {
        $html = '<figure ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">'
                . do_shortcode( $content ) . '<figcaption class="image__slide-meta">';
        $html .= ( isset( $atts['caption'] ) && '' != $atts['caption'] ) ? '<span class="caption__text">' . $atts['caption'] . '</span>' : '';
        $html .= ( isset( $credits ) && '' != $credits) ? '<span class="caption__source">'.$credits.'</span> ' : '';
        $html .= ( isset( $date) && '' != $date ) ? '<span class="caption__date">'.$date.'</span>' : '';
        $html .= '</figcaption></figure>';

    } else {
        $html = '<div ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">'
                . do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>';
    }

    return $html;

}
add_filter( 'img_caption_shortcode', 'update_caption_shortcode', 10, 3 );

I’ve also tried to edit that function in order to display credits/date even if the caption is not set but couldnt do it.

Read more here: Change images html


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: