Advertisement
alchymyth

image title on caption

Jun 28th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. remove_shortcode('wp_caption');
  2. remove_shortcode('caption');
  3. add_shortcode('wp_caption', 'custom_img_caption_shortcode');
  4. add_shortcode('caption', 'custom_img_caption_shortcode');
  5.  
  6. function custom_img_caption_shortcode($attr, $content = null) {
  7. //output image title above caption field//
  8.     // New-style shortcode with the caption inside the shortcode with the link and image tags. wp3.4
  9.     if ( ! isset( $attr['caption'] ) ) {
  10.         if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
  11.             $content = $matches[1];
  12.             $attr['caption'] = trim( $matches[2] );
  13.         }
  14.     }
  15.  
  16.     extract(shortcode_atts(array(
  17.         'id'    => '',
  18.         'align' => 'alignnone',
  19.         'width' => '',
  20.         'caption' => ''
  21.     ), $attr));
  22.  
  23.     if ( 1 > (int) $width || empty($caption) )
  24.         return $content;
  25.  
  26.     if ( $id ) { $img_id = array_pop(explode('_',$id)); $id = 'id="' . esc_attr($id) . '" '; }
  27.    
  28.     $framewidth = 5; // frame width in px per side; default is 5 //
  29.  
  30.     $output =  '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . ( 2 * $framewidth + (int) $width) . 'px">';
  31.     $output .= do_shortcode( $content );
  32.    
  33.     $title = ''; //extract the image title with preg_match_all from $content
  34.     if ( preg_match_all( '#(title=")(.*)(" )#U', $content, $matches, PREG_PATTERN_ORDER ) ) {
  35.         $title = str_replace(array('title="','"'),'',$matches[0][0]);
  36.         }
  37.     if( $title ) $output .= '<span class="caption-image-title">' . $title . '</span>';
  38.    
  39.     $output .= '<p class="wp-caption-text">' . $caption . '</p></div>';
  40.     return $output;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement