How to display a shortcode caption somewhere other than the_content

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.

Answers 1

  • 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).


Related Questions