Advertisement
jan_dembowski

caption-class.php

Apr 22nd, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Add credit-class to image captions
  4. Description: By adding ":credits-caption:" into your image caption, the class "credits-caption" will get added to the div surrounding the image.
  5. Author: Jan Dembowski
  6. Author URI: http://blog.dembowski.net/
  7. */
  8.  
  9. add_filter( 'img_caption_shortcode', 'mh_caption_class_hack', 10, 3 );
  10. function mh_caption_class_hack( $output, $attr, $content ) {
  11.  
  12.         if ( $output != '' )
  13.                 return $output;
  14.  
  15.         extract(shortcode_atts(array(
  16.                 'id'    => '',
  17.                 'align' => 'alignnone',
  18.                 'width' => '',
  19.                 'caption' => ''
  20.         ), $attr));
  21.  
  22.         if ( 1 > (int) $width || empty($caption) )
  23.                 return $content;
  24.  
  25.         if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
  26.  
  27.         if ( !preg_match( '/:credits-caption:/' , $caption . '/' , $mh_dont_care ) ) {
  28.                 return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
  29.                 . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
  30.         } else {
  31.                 $caption = str_replace( ':credits-caption:', '' , $caption );
  32.                 return '<div ' . $id . 'class="wp-caption credits-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
  33.                 . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
  34.         }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement