Title: Get header image description to string
Last modified: August 20, 2016

---

# Get header image description to string

 *  Resolved [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/)
 * Default code for header image in **TwentyTen** is
    `<img src="<?php header_image();?
   >" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT;?
   >" alt="" />`
 * Now I wish to get header image description and use it for _ALT_ tag.
 * The question is: How to get header image description? In functions.php I added
   this:
 *     ```
       'footonsand' => array(
       			'url' => '%s/images/headers/sand_and_foot.jpg',
       			'thumbnail_url' => '%s/images/headers/sand_and_foot-thumbnail.jpg',
       			'description' => __( 'Sand and Foot by Aleksandar Urošević', 'twentyten' )
       		),
       ```
   
 * and I wish to get this description _Sand and Foot by Aleksandar Urošević_ to 
   some string, for example to $alt, and then echo $alt to page.
 * TIA

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/get-header-image-description-to-string/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/get-header-image-description-to-string/page/2/?output_format=md)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2240882)
 * do you mean:
 * `get_bloginfo('description')`
 * [http://codex.wordpress.org/Function_Reference/get_bloginfo](http://codex.wordpress.org/Function_Reference/get_bloginfo)
 *  Thread Starter [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2240885)
 * No, get_bloginfo(‘description’) returns blog tagline. I need description defined
   in functions.php file for selected header image. Same text that is displayed 
   as tooltip when we hover some header image thumbnail on Headers admin page.
 *  Thread Starter [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2240897)
 * OK, I take a look on `wp-admin/custom-header.php` and `wp-includes/header-image.
   php`, and got this dirty workaround:
 *     ```
       function get_header_image_desc() {
       	global $_wp_default_headers;
       	$header_description = get_bloginfo('description');
       	$header_image = str_replace( get_template_directory_uri(), "", get_theme_mod('header_image') );
       	foreach ( $_wp_default_headers as $header_key => $header ) {
       		if ( str_replace("%s", "", $header['url']) == $header_image ) {
       			$header_description = $header['description'];
       		}
       	}
       	return $header_description;
       }
   
       function header_image_desc() {
       	echo get_header_image_desc();
       }
       ```
   
 * Now in place where I wish to put header image description just insert `header_image_desc()`
   and homework is done.
 * Now this `get_image_desc()` function need to be enhanced to get description from
   attachment image, if needed.
 *  [Si Kord](https://wordpress.org/support/users/_smn/)
 * (@_smn)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241243)
 * Hi urkekg,
 * thanks for this, works for static header images for me. But it does not for random
   headers (like the random ones, wp offers, so no uploaded ones but hardcoded in
   functions.php)
 * Do you have a way how that could work? I always just get the `get_bloginfo('name').":".
   get_bloginfo('description');` part but no description is used.
 * I tried to go through that but I’m still confused by the different variables…
 * Thanks!
 *  Thread Starter [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241245)
 * Sorry but right now, when random header image is active, we can’t get which image
   is chosen outside `header_image()` call (part of `theme.php` core file), so we
   can’t get description from header images array.
 *  [Si Kord](https://wordpress.org/support/users/_smn/)
 * (@_smn)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241246)
 * Hi, thanks for your reply.
 * I mean we get the image url as return from the header_image(). Couldn’t it work
   somehow to cross-check that url matching one of the array entries and get this
   description then?
 * Sorry for not proposing any code – the result of a combination of being short
   in time and being a php noob…
 *  Thread Starter [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241247)
 * Well, header_image() echoes image URL, but I think that we can set URL to variable
   with get_header_image(), and then echo that URL for src attrib, and call modified
   get_header_image_desc() with that URL as param.
 * I’ll post code latter from laptop, now I’m on my phone.
 *  [Si Kord](https://wordpress.org/support/users/_smn/)
 * (@_smn)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241248)
 * Cool, thank you!
 * looking forward to try that out.
 *  [Si Kord](https://wordpress.org/support/users/_smn/)
 * (@_smn)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241249)
 * Hi urkekg,
 * do you have that code for me? Did it work that way?
 *  Thread Starter [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241251)
 * OK, here is dirty workaround (please first make backup of your **function.php**
   and **header.php** files from theme directory, if something goes wrong) 🙂
 * In **functions.php** add new function:
 *     ```
       function my_get_random_header_image() {
       	global $_wp_default_headers;
   
       	$header_image_mod = get_theme_mod( 'header_image', '' );
       	$headers = array();
   
       	if ( 'random-uploaded-image' == $header_image_mod )
       		$headers = get_uploaded_header_images();
       	elseif ( ! empty( $_wp_default_headers ) ) {
       		if ( 'random-default-image' == $header_image_mod ) {
       			$headers = $_wp_default_headers;
       		} else {
       			$is_random = get_theme_support( 'custom-header' );
       			if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
       				$headers = $_wp_default_headers;
       		}
       	}
   
       	if ( empty( $headers ) )
       		return '';
   
       	$random_image = array_rand( $headers );
       	$header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
   
       	define( 'HEADER_IMAGE_DESC', $headers[$random_image]['description'] );
   
       	return $header_url;
       }
       ```
   
 * Then in **header.php** replace line
    `<img src="<?php header_image(); ?>" width
   ="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?
   >" alt="<?php header_image_desc(); ?>" title="<?php header_image_desc(); ?>" /
   >` with this code:
 *     ```
       if ( is_random_header_image() ) { ?>
       <img src="<?php echo my_get_random_header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="<?php echo HEADER_IMAGE_DESC; ?>" title="<?php echo HEADER_IMAGE_DESC; ?>" />
       <?php } else { ?>
       <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="<?php header_image_desc(); ?>" title="<?php header_image_desc(); ?>" />
       <?php } ?>
       ```
   
 * I hope this will help you.
 *  [Si Kord](https://wordpress.org/support/users/_smn/)
 * (@_smn)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241252)
 * Cool, thanks! That works perfectly for me!
 *  [photocurio](https://wordpress.org/support/users/photocurio/)
 * (@photocurio)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241255)
 * hm.. can’t get this to work. no descriptions show up.
 * site is here: [http://maureenwhitingco.org/](http://maureenwhitingco.org/)
 *  Thread Starter [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241256)
 * As I noticed in my initial post, HID takes description from **functions.php**
   hardcoded descriptions and nothing to do with image description from media library.
   So, you need to set description like this:
 * `'footonsand' => array(
    'url' => '%s/images/headers/sand_and_foot.jpg', 'thumbnail_url'
   => '%s/images/headers/sand_and_foot-thumbnail.jpg', 'description' => __( 'Sand
   and Foot by Aleksandar Urošević', 'twentyten' ) ),
 * For post image header descriptions WP already work w/o HID.
 *  [photocurio](https://wordpress.org/support/users/photocurio/)
 * (@photocurio)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241257)
 * hey thanks, urkeg. I think I get it now. but my code editor doesn’t like the 
   footonsand array.
 * Is that really the whole code snippet, starting with the single quote and ending
   with the comma?
 *  Thread Starter [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * (@urkekg)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/#post-2241258)
 * Well, that is only a middle part for one image of full array for all header images.
   Comma on the end isays that after that image we have another one. Here is shortened
   full syntax for image header array in functions.php:
 *     ```
       // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
       	register_default_headers( array(
       		'gnu' => array(
       			'url' => '%s/images/headers/bluewildebeestakagnubyjonmountjoy.jpg',
       			'thumbnail_url' => '%s/images/headers/bluewildebeestakagnubyjonmountjoy-thumbnail.jpg',
       			/* translators: header image description */
       			'description' => __( 'Blue Wildebeest aka Gnu by Jon Mountjoy', 'twentyten' )
       		),
       		'berries' => array(
       			'url' => '%s/images/headers/berries.jpg',
       			'thumbnail_url' => '%s/images/headers/berries-thumbnail.jpg',
       			/* translators: header image description */
       			'description' => __( 'Berries', 'twentyten' )
       		),
       		'footonsand' => array(
       			'url' => '%s/images/headers/sand_and_foot.jpg',
       			'thumbnail_url' => '%s/images/headers/sand_and_foot-thumbnail.jpg',
       			/* translators: header image description */
       			'description' => __( 'Sand and Foot by Aleksandar Urošević', 'twentyten' )
       		),
       		'mercado_navideno' => array(
       			'url' => '%s/images/headers/mercado_navideno.jpg',
       			'thumbnail_url' => '%s/images/headers/mercado_navideno-thumbnail.jpg',
       			/* translators: header image description */
       			'description' => __( 'Mercado navideño by José Luis Ruiz', 'twentyten' )
       		)
       	) );
       ```
   

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/get-header-image-description-to-string/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/get-header-image-description-to-string/page/2/?output_format=md)

The topic ‘Get header image description to string’ is closed to new replies.

## Tags

 * [alt](https://wordpress.org/support/topic-tag/alt/)
 * [description](https://wordpress.org/support/topic-tag/description/)
 * [header image](https://wordpress.org/support/topic-tag/header-image/)
 * [random header image](https://wordpress.org/support/topic-tag/random-header-image/)

 * 17 replies
 * 4 participants
 * Last reply from: [Aleksandar Urošević](https://wordpress.org/support/users/urkekg/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/get-header-image-description-to-string/page/2/#post-2241260)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
