WordPress.org

Make WordPress Core

Ticket #15787: caption_change.txt

File caption_change.txt, 1.3 KB (added by PoPSiCLe, 4 years ago)

This is a code-example taken from my theme's functions.php-file, hence it's a replacement function, it should "pop in" directly and replace the original one, though. CSS is included

Line 
1/* This removes the built-in caption shortcodes, and replaces them with XHTML-compliant, valid code */
2remove_shortcode( 'caption' );
3remove_shortcode('wp_caption');
4
5function img_caption_shortcode_self($attr, $content = null) {
6
7        // Allow plugins/themes to override the default caption template.
8
9        $output = apply_filters('img_caption_shortcode_self', '', $attr, $content);
10        if ( $output != '' )
11                return $output;
12        extract(shortcode_atts(array(
13                                                                                                'id'    => '',
14                                                                                                'align' => 'alignnone',
15                                                                                                'width' => '',
16                                                                                                'caption' => ''
17                                                                                        ), $attr));
18
19        if ( 1 > (int) $width || empty($caption) )
20                return $content;
21
22        if ( $id ) $id = 'id="' . $id . '" ';
23                return ' <span ' . $id . 'class="wp-caption ' . $align . '" style="width: ' . (10 + (int) $width) . 'px">'. $content . '<span class="wp-caption-text">' . $caption . '</span></span>';
24}
25
26add_shortcode('wp_caption', 'img_caption_shortcode_self');
27add_shortcode('caption', 'img_caption_shortcode_self');
28
29
30
31//CSS NEEDED FOR THIS TO WORK PROPERLY
32.wp-caption {
33text-align: center;
34padding-top: 4px;
35}
36.wp-caption img {
37margin: 0 auto;
38padding: 0;
39border: 0 none;
40}
41.wp-caption .wp-caption-text {
42display: block;
43font-size: 11px;
44line-height: 22px;
45margin: 0;
46padding: 0 3px 3px 0;
47color: #fff;
48text-align: center;
49}