How to add a link in a image’s caption?

I would like to add a link to the caption of a photo in one of my posts. I can type in the HTML for the link in the caption, but when I publish the post the link gets removed.

How do you add a link to a caption? This would be really useful for giving photo credit.

Related posts

Leave a Reply

4 comments

  1. I wanted to be able to use the media attachment’s title, description, and maybe even the permalink and image link too. I felt that a caption specific hook should be added to the media.php file.

    When I went to submit an enhancement request, it looked like someone beat me to it. Unfortunately, that was four years ago. I bumped it.

    In the meantime, I published the GIC plugin to add this caption hook for me. Once the plugin is installed, you can write a filter to use more media attachment meta as your caption. With a filter, you can even add more styling or remove/hide the caption completely.

    Here’s an example filter that pulls in the image’s title, caption, description, creates a download link, and adds some custom styles.

    /**
     * Custom Filter for Gallery Image Captions
     */
    function my_gallery_image_caption($attachment_id, $captiontag, $selector, $itemtag) {
    
        $id = $attachment_id;
    
        $my_image_meta = galimgcaps_get_image_meta($id);
    
        return "<{$captiontag} class='wp-caption-text gallery-caption' id='{$selector}-{$id}'>"
        . "<strong>" .  wptexturize($my_image_meta['title']) . "</strong><br><br>"
        .  wptexturize($my_image_meta['caption']) . "<br><br>"
        .  wptexturize($my_image_meta['description']) . "<br><br>"
        . "<strong>Alt Text</strong>: " . $my_image_meta['alt'] . "<br><br>"
        . "<a href='" . $my_image_meta['src'] . "' target='_blank'>Download Image</a>"
        . "</{$captiontag}></{$itemtag}>";
    
    }
    add_filter('galimgcaps_gallery_image_caption', 'my_gallery_image_caption', 10, 4);
    

    Check out the documentation and examples to see if this works for you. Shout if you have any questions.