Advertisement
alchymyth

link caption text to link of caption image

May 27th, 2011
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. add_shortcode('wp_caption', 'linktxt_img_caption_shortcode');
  2. add_shortcode('caption', 'linktxt_img_caption_shortcode');
  3. function linktxt_img_caption_shortcode($attr, $content = null) {
  4.  // Allow plugins/themes to override the default caption template.
  5.  $output = apply_filters('img_caption_shortcode', '', $attr, $content);
  6.  if ( $output != '' )
  7.  return $output;
  8.  
  9.  extract(shortcode_atts(array(
  10.  'id'    => '',
  11.  'align'    => 'alignnone',
  12.  'width'    => '',
  13.  'caption' => ''
  14.  ), $attr));
  15.  
  16.  if ( 1 > (int) $width || empty($caption) )
  17.  return $content;
  18.  
  19.  if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
  20.  
  21.  $frame_width = 5; // frame width in pixels per side //
  22. global $post;
  23. preg_match('#(href=\")(.*)(\">)#e',$content,$link); //extract any link from the image
  24. if( $link[2]) :
  25.  return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . ( 2 * $frame_width + (int) $width) . 'px">'
  26.  . do_shortcode( $content ) . '<p class="wp-caption-text"><a href="' . $link[2] . '">' . $caption . '</a></p></div>';
  27.  else :
  28.   return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . ( 2 * $frame_width + (int) $width) . 'px">'
  29.  . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
  30. endif;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement