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:
<figure style="width: 300px" class="wp-caption alignnone"><a href="http://example.com/nimoy" rel="nofollow noreferrer noopener"><img src="https://example.com/uploads/nimoy.jpg" alt="Leonard Nimoy in a black suit" title="Leonard Nimoy" width="300" height="228" /></a><figcaption class="wp-caption-text">Leonard Nimoy has done far more thing than just play Mr. Spock</figcaption></figure>
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:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0