21 | Hyperlinks in wordpress image captions

one million dollars, copyright © silkandart, a source of inspirational art on silk
have a look at the image above and check the link within the image caption.
nice feature?
as many of you might have experienced before, the image caption function of wordpress filters all html content, including any attempts to insert links.
however, this functionality would come very handy, if you want to credit the copyright owner of the image (which could obviously be yourself) by providing a one-click connection to their website.
the convention to insert the link into the caption text is:
‘text before the link ##http://www.linkurl.com##link text## text after the link’.
multiple links are possible.
i have choosen the double ## because it does not get filtered from the caption text and is very unlikely to be part of any normal caption.
here follows the code of the function:
add_shortcode('wp_caption', 'LinkInCaption');
add_shortcode('caption', 'LinkInCaption');
function LinkInCaption($attr, $content = null) {
// Allow plugins/themes to override the default caption template.
// to place a link in the caption you need to use following convention:
// 'text before link ##http://linkurl##linktext## text after link ' //
// more than one link allowed
// this function DOES NOT CHECK FOR CORRECT SYNTAX, i.e. the correct number of ##
// thanks to: @sagemintblue http://wordpress.org/support/profile/3715429 for the main function code
// text manipulation by: @alchymyth
$output = apply_filters('img_caption_shortcode', '', $attr, $content);
if ( $output != '' ) return $output;
extract(shortcode_atts(array(
'id'=> '',
'align' => 'alignnone',
'width' => '',
'caption' => ''), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
$text=explode('##',$caption);
if( count($text) >= 3 ) :
$outtext=$text[0]; //start with the caption text to the left of the first ##
for($i=1; $i<count($text); $i=$i+3):
// add the link html codes to the respective places
$outtext = $outtext.'<a href="'.$text[$i].'">'.$text[$i+1].'</a>'.$text[$i+2];
endfor;
$caption = $outtext;
endif; // end of the caption manipulation
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align)
. '" style="width: ' . ((int) $width+10) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">'
. $caption . '</p></div>';
}
don’t worry, the caption will look awful in the editor window, but should render nicely in the browser.
a very small side effect at this stage is that the image caption text appears in the alt text of the image the way you type it, including ## and link url.
the plugin does not check for the correctness of the input.
i would like to encourage you to explore the possibilities of the wordpress filters and functions on your own – nearly any idea can be realized by exploiting them.
be safe and use a virus scanner before downloading or installing any software
i managed to put the function into a plugin – HyperlinkCaption.zip – under the terms of the GNU General Public License:
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
code is poetry – code is alchemy
tagged: blogging software . caption . filter function . hyperlinks . image caption . link in caption . php function . plugin . string manipulation . theme modification . Wordpress