-
-
I’m not really fond with the new image caption short code. The caption template is not usable for me and for most of WordPress savvy user out there.WP caption structure
<div style="width: 169px" class="wp-caption alignnone" id="attachment_14"> <a rel="attachment wp-att-14" href="http://www.whatever.com/attachment/"> <img width="159" height="300" class="size-medium wp-image-14" title="Lorem ipsum" alt="Lorem ipsum" src="http://www.whatever.com/image.png"/> </a> <p class="wp-caption-text">Lorem ipsum</p> </div>
From the above HTML code the whole image content is wrap using a block elements "<div>".
The issue
If the below condition is met it will render the whole document invalid.
- Image caption is placed inside a paragraph.
- Wordpress wpautop (default filters) is enabled; wpautop will auto append <p> on raw text content.
Turn it off
Special Constant
Interestingly a simple caption shortcode has a user defined constant. It seem like WP developer has predict that their implementation is highly debatable.
Alternatively you have the options to hardcode the below Constant to disabled the “Auto Caption” features inside wp-config.php.
define('CAPTIONS_OFF',1); // disabled auto image captionWP Caption shortcode filters
It has filters too, img_caption_shortcode. For advance WP user you can bind this hook to overwrite the default caption template.
Workaround
My workaround involved 1. removing the caption shortcode 2. make a new one. I did this because I don’t like the img_caption_shortcode filters as seem like too much of work.
-
First we recreate/replicate the caption shortcode functions. Named it nwp_caption_shortcode - new wp caption shortcode
function nwp_caption_shortcode($attr, $content=null){ if ( defined('CAPTIONS_OFF') ){ // no check for bool its literally meant off/ get off return $content; } extract(shortcode_atts(array( 'id' => '', 'align' => 'alignnone', 'width' => '', 'caption' => '' ), $attr)); if ( 1 > (int) $width || empty($caption) ){ return $content; } if ( $id ) $id = 'id="' . $id . '" '; $output = '<span ' . $id . 'class="wp-caption ' . $align . '" '; $output .= 'style="width: ' . (10 + (int) $width) . 'px;display:block">'; $output .= $content; $output .= '<dfn class="wp-caption-text">' . $caption . '</dfn></span>'; return apply_filters('nwp_caption_shortcode',$output); }The shortcode functions is pretty much the same, the only different is I used <span> to wrap the image content and <dfn> to hold the caption text.
-
Next create a function to unregister the default caption shortcode.
function remove_caption_shortcode(){ foreach(array('wp_caption','caption') as $tag) { remove_shortcode($tag); } } -
Lastly we register all this functions.
if (version_compare($GLOBALS['wp_version'], '2.6', '>=')){ add_action('init','shortcode_init'); } function shortcode_init(){ add_action('loop_start','remove_caption_shortcode',10); add_action('loop_start','reg_shortcode',11); } function reg_shortcode(){ add_shortcode('caption','nwp_caption_shortcode'); add_shortcode('wp_caption','nwp_caption_shortcode'); }
Download
-
- July 23, 2008 at 1:47 pm
- July 24, 2008 at 5:14 am
- 0.3
- url
-
-
-
3 Responses to “Workaround for WP Image Caption”
Trackback URL: Use the TrackBack url ↑ to ping this article. If your blog does not support Trackbacks you might want to leave a comment instead.
-
-
"write as if you were talking to a good friend (in front of your mother)."
.haveyoursay
Disclaimer: For any content that you post, you hereby grant to Kaizeku Ban the royalty-free, irrevocable, perpetual, exclusive and fully sublicensable license to use, reproduce, modify, adapt, publish, translate, create derivative works from, distribute, perform and display such content in whole or in part, world-wide and to incorporate it in other works, in any form, media or technology now known or later developed. Some rights reserved.
-
Now how do you use it and make it work? Don't laugh. I'm interested and trying to get you to elaborate like a PSDTUTS tutorial.
its a plugin for WP, try "click" the download button
Hi Avice
Nice workaround. I assume that, if you add your workaround code to functions.php, this overrides the need to switch off captions in wp-config.php?
Alastair