Instantly share code, notes, and snippets.

Embed
What would you like to do?
add source url to image captions in Wordpress
// add source url to image captions
// --------------------------------
//
// add this to functions.php of your (child) theme
// original by Kaspars Dambis: https://kaspars.net/blog/wordpress/how-to-automatically-add-image-credit-or-source-url-to-photo-captions-in-wordpress
// modified by Kasper Kamperman: https://www.kasperkamperman.com/blog/source-link-captions-in-wordpress/
add_filter("attachment_fields_to_edit", "add_image_source_url", 10, 2);
function add_image_source_url($form_fields, $post) {
$form_fields["source_url"] = array(
"label" => __("Source URL"),
"input" => "text",
"value" => get_post_meta($post->ID, "source_url", true),
"helps" => __("Add the URL where the original image was posted"),
);
$form_fields["source_credit"] = array(
"label" => __("Source Credits"),
"input" => "text",
"value" => get_post_meta($post->ID, "source_credit", true),
"helps" => __("If you add credits, those will be linked instead of the whole caption."),
);
return $form_fields;
}
add_filter("attachment_fields_to_save", "save_image_source_url", 10 , 2);
function save_image_source_url($post, $attachment) {
if (isset($attachment['source_url']))
update_post_meta($post['ID'], 'source_url', trim($attachment['source_url']));
if (isset($attachment['source_credit']))
update_post_meta($post['ID'], 'source_credit', trim($attachment['source_credit']));
return $post;
}
add_filter('img_caption_shortcode', 'caption_shortcode_with_credits', 10, 3);
function caption_shortcode_with_credits($empty, $attr, $content) {
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr));
// Extract attachment $post->ID
preg_match('/\d+/', $id, $att_id);
if (is_numeric($att_id[0]) && $source_url = get_post_meta($att_id[0], 'source_url', true)) {
if($source_credit = get_post_meta($att_id[0], 'source_credit', true)) {
$caption .= ' &#8211; <a class="source-credit" href="'. $source_url .'">'. $source_credit .'</a>';
}
else {
$caption = '<a href="'. esc_url ( $source_url ) .'">'. $caption .'</a>';
}
}
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id )
$id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}
// end add source url to image captions
// ------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment