I want to disable shortcode captions for posts in one of my themes, and display the content elsewhere, such as in a sidebar.

The images aren't uploaded to wordpress, but they're linked-to using the post editor's add an image -> from URL functionality resulting in the following shortcode:

[caption id="" align="alignnone" width="300" caption="Leonard Nimoy has done far more thing than just play Mr. Spock"]<a href="http://example.com/nimoy"><img src="http://example.com/uploads/nimoy.jpg" alt="Leonard Nimoy in a black suit" title="Leonard Nimoy" width="300" height="228" /></a>[/caption]

Any thoughts? I'm assuming using a filter on img_caption_shortcode somehow, but I don't know if that's the way to approach this.


Try this:

$caption_info = array();

add_filter( 'img_caption_shortcode', 'capture_caption', 10, 3 );

function capture_caption( $blank = '', $attr, $content ) {

    global $caption_info;

    $caption_info[] = array('attr' => $attr, 'content' => $content );
    return ' ';

It will save info from all captions into global $caption_info variable and suppress their display in content (space is returned because filter result is ignored if empty).

  • Sweet. I knew that would work somehow. Should I add a conditional is_singular() to this? – Dan Gayle Sep 14 '10 at 16:33
  • Well, that depends on how exactly you want it to work. In this form it will fire on every call of img_caption_shortcode() function that powers [caption] and [wp_caption] shortcodes. If you want to skip on some conditions - check for them and return empty '' in function. – Rarst Sep 14 '10 at 16:43
  • I've put that in, but doing global $caption_info; var_dump($caption_info); in my single.php results in array(0) {}. Thoughts? – Dan Gayle Sep 14 '10 at 20:10
  • Dumb moment, but I tripped on that myself, so... Variable is filled only after content is displayed and shortcodes run. Are you doing this after the_content()? – Rarst Sep 14 '10 at 20:16
  • I just figured this out myself, and was about to ask that question, lol. Doing this: $content = apply_filters('the_content',get_the_content()); earlier in the page helps. Then I can simply echo $content where the_content() would be. It still feels like a hack though. Any way to get that filter to run earlier? – Dan Gayle Sep 14 '10 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.