Opened 14 years ago
Closed 10 years ago
#16268 closed enhancement (fixed)
Requirement for has_header_image() function?
Reported by: |
|
Owned by: |
|
---|---|---|---|
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)
Change History (15)
#3
@
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.
#6
@
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
@
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)).
#8
@
10 years ago
Refreshed patch for current trunk, made patch from root directory, and updated to match coding standards.
For now you can use this:
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):
Although I don't prefer this way of checking since it would call
get_header_image()
twice, it works.