Created
September 8, 2011 13:38
-
-
Save mfields/1203408 to your computer and use it in GitHub Desktop.
Get Background Color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function mytheme_get_background_color() { | |
| $color = get_background_color(); | |
| if ( ! ctype_xdigit( $color ) ) | |
| return 'transparent'; | |
| if ( ! in_array( strlen( $color ), array( 3, 6 ) ) ) | |
| return 'transparent'; | |
| return '#' . $color; | |
| } |
Author
Never used one in this context before. How would it work?
HEADDESK
No need for a switch. Try this:
if( ctype_xdigit($color) && ( (3 == strlen($color)) || (6 == strlen($color)) ){
return '#' . $color;
} else {
return 'transparent';
}
The above should work since ctype_xdigit() will return FALSE if it detects a null (i.e., no color set).
Author
Great call with the empty check. It's probably not needed at all. I always miss these little opportunities to optimize.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not use a switch statement?