Add Description Field To Images In WordPress

When adding an image via the default WordPress image uploader, one option among many is the ability to define a description….


Interestingly enough, when embedding the image inside of a post, the description field unlike the caption field does not display by default.  Recently for a project, we needed to utilize the description field to dump in additional photo information, while the caption field was used to provide the regular text caption.

To get WordPress to display the description inside the post we had to override the the default image shortcode function WordPress used to generate the final HTML output for the images inside posts.  Then we needed to make some changes to our stylesheet and we were set. Here’s a step by step:

1. Drop this into your functions.php file…

add_shortcode('wp_caption', 'custom_img_caption_shortcode');
add_shortcode('caption', 'custom_img_caption_shortcode');

function custom_img_caption_shortcode($attr, $content = null) {

	// Allow plugins/themes to override the default caption template.
	$output = apply_filters('img_caption_shortcode', '', $attr, $content);
	if ( $output != '' )
		return $output;

	extract(shortcode_atts(array(
		'id'	=> '',
		'align'	=> 'alignnone',
		'width'	=> '',
		'caption' => ''
	), $attr));

	if ( 1 > (int) $width || empty($caption) )
		return $content;

	// Grab ID to query post_content [description] field for image
	preg_match('/([\d]+)/', $id, $matches);

	$description = '';
	if ($matches[0]) {

		global $wpdb;
		$custom_description = $wpdb->get_row("SELECT post_content FROM $wpdb->posts WHERE ID = {$matches[0]};");

		if ($custom_description->post_content) {

			$description = '<p class="wp-custom-description">'. $custom_description->post_content . '</p>';

		}
	}

	if ( $id ) $id = 'id="' . esc_attr($id) . '" ';

	return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
	. do_shortcode( $content ) . $description . '<p class="wp-caption-text">' . $caption . '</p></div>';
}

If you look at this line…

$description = '<p class="wp-custom-description">'. $custom_description->post_content . '</p>';

…you’ll notice where we dumped the description into a paragraph with a class of “wp-custom-description”. Feel free to modify the code to wrap whatever HTML around the description you need (div, span etc), in whatever location you need.

2. Edit your CSS file to style your new description…

 p.wp-custom-description { /*Some CSS Goodness Here */ } 

That’s it, you’re done! Here’s an example of the description in action….

Photo Location: North Pole

Three Penguins Discussing The Economic Stimulus Package


This entry was posted in Tips & Tricks, Wordpress. Bookmark the permalink.

5 Responses to Add Description Field To Images In WordPress

  1. Jason Paul says:

    Great function. I’m in a situation where I need to add the description to old content where images are already embedded. Anyway to get the image description to show up on the edit image popup? For some reason it’s different than the add image pop up.

    • Alisa says:

      I second that question. If you don’t enter the description with the original “add image”, when you edit the image, adding or changing a description is not an option. Is editing html the only option?

  2. Mel says:

    Hey, das sollte eigentlich super klappen. Aber dennoch funktionierts bei mir nicht.

    Ich benutzt ein Child Theme und hab den code ganz normal in die functions.php eingefügt. Dann hab ich über den regulären editor ein bild eingebunden mit description etc.

    Wisst ihr vielleicht worans liegen könnte? Danke

  3. I am kind of stuck on this as well. I need the description only for an image gallery and then use the attachments to the page to actually describe the galleries. Like a short code for a gallery, puts the id’s of each image, and then it grabs the description and title for them.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>