Shortcode function doesnt work without caption

I’m working on a site that has this function to output the caption of a picture and is credits/date but doesn’t work if only credits/date are definied and caption isnt. If thats the case the function will ignore the image and it wil be wrapped in a p tag which i think its generated by wordpress by default and i cant even understand where that code is being credit. Is there a way to make this function output the credits/date without the caption?

    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' );
    $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 );

Read more here: Shortcode function doesnt work without caption

Leave a Reply

Your email address will not be published. Required fields are marked *