How to always add caption element to images – even if empty?

This works: add_filter( ‘disable_captions’, create_function(‘$a’, ‘return true;’) ); function image_send_to_editor_2($html, $id, $caption, $title, $align, $url, $size, $alt) { $width=”auto”; if ( preg_match( ‘/width=”([0-9]+)/’, $html, $matches ) ) { $width = $matches[1] . ‘px’; } $output=”<div id=”attachment-” . $id . ‘” class=”wp-caption align’ . $align . ‘” style=”width: ‘ . $width . ‘;”>’; $output .= $html; … Read more

Remove wp-caption but still show the image

You’ll need to use the img_caption_shortcode filter to do this. Pretty certain the alignment of the image (floating left/right) is part of the caption so you’ll lose the ability to do that with this code. function imageOnly($deprecated, $attr, $content = null) { return do_shortcode( $content ); } add_filter( ‘img_caption_shortcode’, ‘imageOnly’, 10, 3 ); The do_shortcode … Read more

How to get the_post_thumbnail caption?

Use the shortcode handler img_caption_shortcode to render the HTML for you, though you do need to pass it a width (which we can easily get with wp_get_attachement_image_src: function wpse_138126_thumbnail_caption( $html, $post_id, $post_thumbnail_id, $size, $attr ) { if ( $post = get_post( $post_thumbnail_id ) ) { if ( $size = wp_get_attachment_image_src( $post->ID, $size ) ) $width … Read more

Can’t insert caption into image

I solved it, the problem was a function I used to use and forgot about! add_filter( ‘post_thumbnail_html’, ‘remove_thumbnail_dimensions’, 10 ); add_filter( ‘image_send_to_editor’, ‘remove_thumbnail_dimensions’, 10 ); function remove_thumbnail_dimensions( $html ) { $html = preg_replace( ‘/(width|height)=\”\d*\”\s/’, “”, $html ); return $html; } I removed this function and everything is OK now.

Showing image description along with embedded links below post images

When you look at the last line img_caption_shortcode(), then you can see wp_kses() stripping non allowed HTML tags there. The wp_kses_hook() function does only wrap a filter: return apply_filters( ‘pre_kses’, $string, $allowed_html, $allowed_protocols ); which allows to hook in and alter the output before wp_kses_split() finally removes the HTML tag by using preg_replace_callback with _wp_kses_split_callback() … Read more

wp_get_attachment_caption to pull Image Caption or alt

You can directly get an attachment’s meta data if you have it’s ID. The alt data for an attachment is stored in _wp_attachment_image_alt. So, you can use: $alt = get_post_meta( $attachment->ID, ‘_wp_attachment_image_alt’, true); To get the caption of an image, you can use wp_get_attachment_metadata(). $metadata = wp_get_attachment_metadata( $id ); $caption = $metadata[‘image_meta’][‘caption’]; EDIT Based on … Read more