Advertisement
sgaffney

figure/figcaption for images

Dec 20th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. add_filter( 'img_caption_shortcode', 'tumble_cleaner_caption', 10, 3 );
  2.  
  3. function tumble_cleaner_caption( $output, $attr, $content ) {
  4.  
  5.     /* We're not worried abut captions in feeds, so just return the output here. */
  6.     if ( is_feed() )
  7.         return $output;
  8.  
  9.     /* Set up the default arguments. */
  10.     $defaults = array(
  11.         'id' => '',
  12.         'align' => 'alignnone',
  13.         'width' => '',
  14.         'caption' => ''
  15.     );
  16.  
  17.     /* Merge the defaults with user input. */
  18.     $attr = shortcode_atts( $defaults, $attr );
  19.  
  20.     /* If the width is less than 1 or there is no caption, return the content wrapped between the [caption]< tags. */
  21.     if ( 1 > $attr['width'] || empty( $attr['caption'] ) )
  22.         return $content;
  23.  
  24.     /* Set up the attributes for the caption <div>. */
  25.     $attributes .= ' class="figure ' . esc_attr( $attr['align'] ) . '" style="width:'. esc_attr( $attr['width'] ) . 'px"';
  26.  
  27.     /* Open the caption <div>. */
  28.     $output = '<figure' . $attributes .'>';
  29.  
  30.     /* Allow shortcodes for the content the caption was created for. */
  31.     $output .= do_shortcode( $content );
  32.  
  33.     /* Append the caption text. */
  34.     $output .= '<figcaption>' . $attr['caption'] . '</figcaption>';
  35.  
  36.     /* Close the caption </div>. */
  37.     $output .= '</figure>';
  38.  
  39.     /* Return the formatted, clean caption. */
  40.     return $output;
  41. }
  42.  
  43. // Clean the output of attributes of images in editor. Courtesy of SitePoint. http://www.sitepoint.com/wordpress-change-img-tag-html/
  44. function image_tag_class($class, $id, $align, $size) {
  45.     $align = 'align' . esc_attr($align);
  46.     return $align;
  47. }
  48.  
  49. add_filter('get_image_tag_class', 'image_tag_class', 0, 4);
  50.  
  51. function image_tag($html, $id, $alt, $title) {
  52.     return preg_replace(array(
  53.             '/\s+width="\d+"/i',
  54.             '/\s+height="\d+"/i',
  55.             '/alt=""/i'
  56.         ),
  57.         array(
  58.             '',
  59.             '',
  60.             '',
  61.             'alt="' . $title . '"'
  62.         ),
  63.         $html);
  64. }
  65. add_filter('get_image_tag', 'image_tag', 0, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement