Make WordPress Core

Opened 14 years ago

Closed 10 years ago

#16268 closed enhancement (fixed)

Requirement for has_header_image() function?

Reported by: husobj's profile husobj Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 4.2 Priority: normal
Severity: normal Version:
Component: Customize Keywords: has-patch
Focuses: template Cc:

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?

Attachments (4)

ticket16268.patch (503 bytes) - added by GunGeekATX 11 years ago.
Proposed patch to implement has_header_image()
16268.patch (488 bytes) - added by GunGeekATX 11 years ago.
16268.2.diff (503 bytes) - added by voldemortensen 10 years ago.
16268.3.diff (509 bytes) - added by GunGeekATX 10 years ago.

Download all attachments as: .zip

Change History (15)

#1 @garyc40
14 years ago

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.

#2 @ramiy
14 years ago

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

+1

#3 @nacin
14 years ago

  • 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.

#4 @nacin
11 years ago

  • Component changed from Template to Appearance
  • Focuses template added

@GunGeekATX
11 years ago

Proposed patch to implement has_header_image()

#5 @GunGeekATX
11 years ago

  • Keywords has-patch added; needs-patch removed

#6 @obenland
11 years ago

Thanks for the patch, looks like it's a good first step in the right direction!

A few things that came to mind:

  • Patches should be created from the root directory of your WordPress SVN install.
  • The function body could probably be simplified by literally casting the return value of get_header_image() to a bool.
  • Feel free to check out the function documentation of other functions in that file, to get a feel for wording and punctuation.
  • The documentation could be misinterpreted as whether a theme has a (registered) custom header. "Checks whether a header image is set." might be more accurate.

#7 @GunGeekATX
11 years ago

Thanks for the feedback. I'll make some changes and resubmit the patch.

I too wanted to implement it as return !empty(get_header_image());, but it would not work in older PHP versions.

http://www.php.net//manual/en/function.empty.php
Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

@GunGeekATX
11 years ago

#8 @voldemortensen
10 years ago

Refreshed patch for current trunk, made patch from root directory, and updated to match coding standards.

#9 @celloexpressions
10 years ago

  • Milestone changed from Future Release to 4.2

Patch needs a space after !, otherwise looks good.

@GunGeekATX
10 years ago

#10 @GunGeekATX
10 years ago

Updated patch

#11 @SergeyBiryukov
10 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 31224:

Introduce has_header_image() to check whether a header image is set.

props GunGeekATX, voldemortensen.
fixes #16268.

Note: See TracTickets for help on using tickets.