I have images in my Wordpress posts with the following field setup:
Caption: This field contains the actual image caption to be displayed below the image.
Description: This field contains photo credits with links to attributed sources.
I need both these values to be displayed right below the image like so:
Taking cue from an excellent answer from @toscho to this question, I tried this function:
add_shortcode('wp_caption', 'img_caption_add_description');
add_shortcode('caption', 'img_caption_add_description');
function img_caption_add_description($attr, $content = null)
{
$post_id = str_replace('attachment_', '', $attr['id']);
$img = get_post((int)$post_id);
if (is_a($img, 'WP_Post')) {
$attr['caption'] = $img->post_content;
}
return img_caption_shortcode($attr, $content);
}
As you can see in the screenshot, the function does its job well except that it strips off all hyperlinks. I need them! Is there any way to modify the above function in a way that it honors the links? In the above example, for instance, the texts "Amit Schandillia" and "CC BY-SA 3.0", both are meant to be hyperlinks. I have the links alright in my Description field as below:
Photo credit: <a href="" target="_blank" rel="nofollow">Amit Schandillia</a> licensed <a href="" target="_blank" rel="nofollow"><i class="fa fa-creative-commons"></i> BY-SA 3.0</a>
How do I make sure the <a>
tags stay when being rendered on the post page?