Opened 2 years ago

Last modified 2 years ago

#16268 new enhancement

Requirement for has_header_image() function?

Reported by: husobj Owned by:
Priority: normal Milestone: Future Release
Component: Template Version:
Severity: normal Keywords: needs-patch
Cc: ramiy

Description

I know is pretty easy to do, but would it be a good idea to provide a has_header_image() function for use in themes? Something along the lines of:

function has_header_image() {
   $header_image = get_header_image();
   return !empty( $header_image );
}

As the header_image() function just outputs the URL it would be handy to have a conditional function you could use to check when there was a header image to output.

I'm currently working on a plugin that gives you more granular control over using different header images on different pages using the 'theme_mod_header_image' filter. There may be instances when you do not want to show a header at all on some pages. In the current TwentyTen theme for example, no checking is done before the header_image() function is called so in this scenario the header image just appears as a broken image - ideally it should not output the image at all.

Either that or should there be a function which outputs the whole image tag so that if the header_image() function returns false/empty it does not output the tag at all?

What are people's thoughts on this?

Change History (3)

For now you can use this:

if ( $header_image = get_header_image() ) {
	// do something when there's a header image defined
}

In TwentyTen, at least in the current trunk version I'm using, there's a check for header image so no broken image is output (line 76 - 79 in header.php):

...
elseif ( get_header_image() ) : ?>
	<img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
<?php endif; ?>
...

Although I don't prefer this way of checking since it would call get_header_image() twice, it works.

  • Cc ramiy added
  • Type changed from defect (bug) to enhancement

+1

  • Keywords needs-patch added; header_image removed
  • Milestone changed from Awaiting Review to Future Release

I wouldn't mind a has_header_image() function for consistency. It should be noted though that get_header_image() is a super fast check, and that has_header_image() would simply return a bool cast of get_header_image() anyway.

Note: See TracTickets for help on using tickets.