The site im working on has a shortcode for captions, but it has a problem, if the caption is not set, but the photographer name and photo date are it won’t display anything so i created a function to post just the photographer name and photo date when the caption is not set. It works well, but if after i insert an image with no caption in a post i change it and add a caption it display both htmls (from the shortcode and my function). I tried adding a return if isset( $caption ) and an empty html variable if the caption is not empty but it didnt work. This is the code:

    function include_image_credits( $html, $id, $alt, $title, $align, $size ) {
        $post = get_post( $id );
        $caption = wp_get_attachment_caption( $id );
        $get_img_info = wp_get_attachment_metadata( $id );
        $img_width = $get_img_info['width'];
        $credits = get_post_meta( $id, 'photographer_name', true );
        $date = get_post_meta( $id, 'photo_date',true );
        $style = '';
    if ( $img_width ) {
            $style = 'style="width: ' . (int) $img_width . 'px" ';
    }
    error_log(print_r($style,true));
    if ( isset( $credits ) && empty( $caption ) && '' !== $credits || isset( $date ) &&  empty( $caption ) && '' !== $date ) {
            $html .= '<div class="mainDiv"' . $style . '>';
            $html .= '<span class="caption__source">' . $credits . '</span> ';
            $html .= '<span class="caption__date">' . $date . '</span> ';
            $html .= '</div>';
    }
    return $html;
}
add_filter( 'get_image_tag', 'include_image_credits', 10, 6 );

How can i make my code “ignore” this function if a caption is set later?

Read more here: getting html from two functions


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: