<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Kaizeku Ban &#187; shortcode</title>
	<atom:link href="http://blog.kaizeku.com/taxonomy/shortcode/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kaizeku.com</link>
	<description>So many evil plans, so little time...</description>
	<pubDate>Wed, 19 Nov 2008 01:02:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7-beta2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Workaround for WP Image Caption</title>
		<link>http://blog.kaizeku.com/wordpress/workaround-for-wp-image-caption/</link>
		<comments>http://blog.kaizeku.com/wordpress/workaround-for-wp-image-caption/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 13:47:02 +0000</pubDate>
		<dc:creator>Avice</dc:creator>
		
		<category><![CDATA[WordPress]]></category>

		<category><![CDATA[2.6]]></category>

		<category><![CDATA[shortcode]]></category>

		<category><![CDATA[wp constant]]></category>

		<category><![CDATA[wp_filters]]></category>

		<guid isPermaLink="false">http://blog.kaizeku.com/?p=211</guid>
		<description><![CDATA[I&#8217;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
&#60;div style&#61;&#34;width&#58; 169px&#34; class&#61;&#34;wp&#45;caption alignnone&#34; id&#61;&#34;attachment_14&#34;&#62;
 &#60;a rel&#61;&#34;attachment wp&#45;att&#45;14&#34; href&#61;&#34;http&#58;&#47;&#47;www&#46;whatever&#46;com&#47;attachment&#47;&#34;&#62;
 &#60;img width&#61;&#34;159&#34; height&#61;&#34;300&#34; class&#61;&#34;size&#45;medium wp&#45;image&#45;14&#34; title&#61;&#34;Lorem ipsum&#34; alt&#61;&#34;Lorem ipsum&#34; src&#61;&#34;http&#58;&#47;&#47;www&#46;whatever&#46;com&#47;image&#46;png&#34;&#47;&#62;
 &#60;&#47;a&#62;
 &#60;p class&#61;&#34;wp&#45;caption&#45;text&#34;&#62;Lorem ipsum&#60;&#47;p&#62;
&#60;&#47;div&#62;
From the above [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.kaizeku.com/wp-content/uploads/2008/07/wordpress-caption-shortcode-usability-pin-preview-by-chaoskaizer.png" alt="wordpress caption shortcode" title="wordpress-caption-shortcode-usability-pin-preview-by-chaoskaizer" width="128" height="128" class="alignright size-full wp-image-212" />I&#8217;m not really fond with the new image <em class="hilite-2">caption</em> short code. The caption template is not usable for me and for most of WordPress <a href="http://wordpress.org/search/image+caption+2.6?forums=1">savvy user out there</a>.</p>
<h2 class="mgt">WP caption structure</h2>
<pre class="prebox">&#60;div style&#61;&#34;width&#58; 169px&#34; class&#61;&#34;wp&#45;caption alignnone&#34; id&#61;&#34;attachment_14&#34;&#62;
 &#60;a rel&#61;&#34;attachment wp&#45;att&#45;14&#34; href&#61;&#34;http&#58;&#47;&#47;www&#46;whatever&#46;com&#47;attachment&#47;&#34;&#62;
 &#60;img width&#61;&#34;159&#34; height&#61;&#34;300&#34; class&#61;&#34;size&#45;medium wp&#45;image&#45;14&#34; title&#61;&#34;Lorem ipsum&#34; alt&#61;&#34;Lorem ipsum&#34; src&#61;&#34;http&#58;&#47;&#47;www&#46;whatever&#46;com&#47;image&#46;png&#34;&#47;&#62;
 &#60;&#47;a&#62;
 &#60;p class&#61;&#34;wp&#45;caption&#45;text&#34;&#62;Lorem ipsum&#60;&#47;p&#62;
&#60;&#47;div&#62;</pre>
<p>From the above HTML code the whole image content is wrap using a block elements <tt>&#34;&#60;div&#62;&#34;</tt>.</p>
<h2 class="mgt">The issue</h2>
<p>If the below condition is met it will render the whole document invalid.</p>
<ol class="xoxo">
<li>Image caption is placed inside a paragraph.</li>
<li>Wordpress wpautop (default filters) is enabled; wpautop will auto append &#60;p&#62; on raw text content.</li>
</ol>
<p><span id="more-211"></span></p>
<h2 class="mgt">Turn it off</h2>
<h5 class="mgt">Special Constant</h5>
<p>Interestingly a simple <strong class="fw-">caption shortcode</strong> has a <em>user defined constant</em>. It seem like WP developer has predict that their implementation is highly debatable.</p>
<p> Alternatively you have the options to hardcode the below Constant to disabled the &#8220;Auto Caption&#8221; features inside <tt>wp-config.php</tt>.</p>
<pre class="smallbox">
define('CAPTIONS_OFF',1); // disabled auto image caption
</pre>
<h5 class="mgt">WP Caption shortcode filters</h5>
<p>It has filters too, <tt class="hilite-4">img_caption_shortcode</tt>. For advance WP user you can bind this hook to overwrite the default caption template.</p>
<h2>Workaround</h2>
<p>My workaround involved 1. removing the caption shortcode 2. make a new one. I did this because I don&#8217;t like the <tt class="hilite-1">img_caption_shortcode</tt> filters as seem like too much of work.</p>
<ol class="xoxo">
<li>
<p>First we recreate/replicate the caption shortcode functions. Named it <tt>nwp_caption_shortcode</tt> - <small>new wp caption shortcode</small> </p>
<pre class="prebox">
function nwp_caption_shortcode&#40;&#36;attr&#44; &#36;content&#61;null&#41;&#123;	

	if &#40; defined&#40;&#39;CAPTIONS_OFF&#39;&#41; &#41;&#123;
		&#47;&#47; no check for bool its literally meant off&#47; get off
		return &#36;content&#59;
	&#125;

	extract&#40;shortcode_atts&#40;array&#40;
		&#39;id&#39;	&#61;&#62; &#39;&#39;&#44;
		&#39;align&#39;	&#61;&#62; &#39;alignnone&#39;&#44;
		&#39;width&#39;	&#61;&#62; &#39;&#39;&#44;
		&#39;caption&#39; &#61;&#62; &#39;&#39;
	&#41;&#44; &#36;attr&#41;&#41;&#59;

	if &#40; 1 &#62; &#40;int&#41; &#36;width &#124;&#124; empty&#40;&#36;caption&#41; &#41;&#123;
		return &#36;content&#59;
	&#125;

	if &#40; &#36;id &#41; &#36;id &#61; &#39;id&#61;&#34;&#39; &#46; &#36;id &#46; &#39;&#34; &#39;&#59;

	&#36;output &#61; &#39;&#60;span &#39; &#46; &#36;id &#46; &#39;class&#61;&#34;wp&#45;caption &#39; &#46; &#36;align &#46; &#39;&#34; &#39;&#59;
	&#36;output &#46;&#61; &#39;style&#61;&#34;width&#58; &#39; &#46; &#40;10 &#43; &#40;int&#41; &#36;width&#41; &#46; &#39;px&#59;display&#58;block&#34;&#62;&#39;&#59;
	&#36;output &#46;&#61; &#36;content&#59;
	&#36;output &#46;&#61; &#39;&#60;dfn class&#61;&#34;wp&#45;caption&#45;text&#34;&#62;&#39; &#46; &#36;caption &#46; &#39;&#60;&#47;dfn&#62;&#60;&#47;span&#62;&#39;&#59;

	return apply_filters&#40;&#39;nwp_caption_shortcode&#39;&#44;&#36;output&#41;&#59;
&#125;
</pre>
<p>The shortcode functions is pretty much the same, the only different is I used <span class="hilite-1">&#60;span&#62;</span> to wrap the image content and <span class="hilite-1">&#60;dfn&#62;</span> to hold the caption text.</p>
</li>
<li>
<p> Next create a function to unregister the default caption shortcode.</p>
<pre class="prebox">
function remove_caption_shortcode&#40;&#41;&#123;
	foreach&#40;array&#40;&#39;wp_caption&#39;&#44;&#39;caption&#39;&#41; as &#36;tag&#41; &#123;
		remove_shortcode&#40;&#36;tag&#41;&#59;
	&#125;
&#125;
</pre>
</li>
<li>
<p> Lastly we register all this functions.</p>
<pre class="prebox">if &#40;version_compare&#40;&#36;GLOBALS&#91;&#39;wp_version&#39;&#93;&#44; &#39;2&#46;6&#39;&#44; &#39;&#62;&#61;&#39;&#41;&#41;&#123;
	add_action&#40;&#39;init&#39;&#44;&#39;shortcode_init&#39;&#41;&#59;
&#125;

function shortcode_init&#40;&#41;&#123;
	add_action&#40;&#39;loop_start&#39;&#44;&#39;remove_caption_shortcode&#39;&#44;10&#41;&#59;
	add_action&#40;&#39;loop_start&#39;&#44;&#39;reg_shortcode&#39;&#44;11&#41;&#59;
&#125;

function reg_shortcode&#40;&#41;&#123;
	add_shortcode&#40;&#39;caption&#39;&#44;&#39;nwp_caption_shortcode&#39;&#41;&#59;
	add_shortcode&#40;&#39;wp_caption&#39;&#44;&#39;nwp_caption_shortcode&#39;&#41;&#59;
&#125;</pre>
</li>
</ol>
<h2 class="mgt">Download</h2>
Note: There is a file embedded within this post, please visit this post to download the file.]]></content:encoded>
			<wfw:commentRss>http://blog.kaizeku.com/wordpress/workaround-for-wp-image-caption/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
