• I’m really confused as to why WordPress offers an image description field, complete with formatable rich text, when it is apparently impossible to display this content within the WordPress system. Is it a vestige of previous versions that programmers forgot to cut out when they rendered it inoperable? There are encyclopedias written on how to display everything from alt and caption text to even ISO and shutter speed, yet the available description field in the Media library seems to be useless. Most pages I find on the matter simply say nope, not possible. All coding attempts you will find online that others have made are woefully broken or simply do nothing. Some come close but they just don’t work. WordPress is an impressively robust system, and yet it’s still somehow missing such obvious elements. I’ve even found a few (failed) attempts by people trying to trick WordPress into thinking the description field is the caption or alt text, because it has no problem displaying those fields. What on earth?

    Does anyone have a clue how to display this evasive text field? Linking to any existing page discussing this issue won’t help, trust me I’ve read them all. If you’re harboring some useful code for this, please please share it with us.

    Thanks,
    Jeremy

Viewing 9 replies - 1 through 9 (of 9 total)
  • It works for me if I call the_content() on an attachment page.

    Thread Starter earthtojeremy

    (@earthtojeremy)

    Yeah, which is no good obviously. We need to be able to display whatever image metadata we need for images in posts. I can’t redirect users to an attachment page every time I want to insert an image.

    Thread Starter earthtojeremy

    (@earthtojeremy)

    Can anyone figure out how to edit the Image Caption Easy plugin to do this? It’s only supposed to display alt text but I’ve managed to get it to also display the image title, but I can’t get it to display image description.

    You can call wp_get_attachment_metadata to get an array with all the metadata for the attachment: width, height, path, etc., and then if you save the results into a variable, you can echo the result anywhere on the page. the_content() doesn’t have anything to do with that.

    Thread Starter earthtojeremy

    (@earthtojeremy)

    I’ve come across this in my searches but again, I could figure out how to display every bit of associated data with it except for description. Do you have any further insight into this? I need this to be how my site automatically handles all images.

    Ah, my apologies, I misunderstood what you were trying to accomplish. Okay, here’s something pretty kludgey.

    function earthtojeremy_description_shortcode( $dummy, $attr, $content ) {
    	global $post;
    
    	$atts = shortcode_atts( array(
    		'id'	  => '',
    		'align'	  => 'alignnone',
    		'width'	  => '',
    		'caption' => ''
    	), $attr, 'caption' );
    
    	$atts['width'] = (int) $atts['width'];
    	if ( $atts['width'] < 1 || empty( $atts['caption'] ) )
    		return $content;
    
    	if ( ! empty( $atts['id'] ) )
    		$atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';
    
    	$caption_width = 10 + $atts['width'];
    
    	$caption_width = apply_filters( 'img_caption_shortcode_width', $caption_width, $atts, $content );
    
    	$style = '';
    	if ( $caption_width )
    		$style = 'style="width: ' . (int) $caption_width . 'px" ';
    
    	$args = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_status' =>'any', 'post_parent' => $post->ID );
    	$attachments = get_posts( $args );
    	if ( $attachments ) {
    		foreach ( $attachments as $attachment ) {
    			$description = $attachment->post_content;
    		}
    	}
    
    	return '<div ' . $atts['id'] . $style . 'class="wp-caption ' . esc_attr( $atts['align'] ) . '">'
    	. do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p><p class="wp-description-text">' . $description . '</p></div>';
    }
    add_filter( 'img_caption_shortcode', 'earthtojeremy_description_shortcode', 10, 3 );

    Copy this into your functions.php file, making a child theme if necessary. Basically what you’re doing here is filtering the output of the caption shortcode to include the image description. You can style the resulting output by targeting wp-description-text in your CSS rules. There’s probably a better way to get the description, but I’d have to work on that a bit.

    Hey, I’ve been working on this same issue.

    I’ve essentially done a similar thing (filtering img_caption_shortcode), but the problem is I have some images which don’t use a caption but have a description. The shortcode only gets called when there’s a caption, so it won’t work if there’s only a description.

    I’m banging my head over this. No image hooks that I can find to hook into.

    I’d like to find out when/how the img_caption_shortcode gets called to see if I can hook in the same way, but haven’t had any luck.

    Any ideas? Has anyone made further progress with this?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I figured, since it was the same issue and apparently unresolved, that it made sense to try to solve this in one place…?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Why is it nearly impossible to display a simple image description?’ is closed to new replies.