Skip to content

Instantly share code, notes, and snippets.

@mfields
Created September 8, 2011 13:38
Show Gist options
  • Select an option

  • Save mfields/1203408 to your computer and use it in GitHub Desktop.

Select an option

Save mfields/1203408 to your computer and use it in GitHub Desktop.
Get Background Color
<?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;
}
@zamoose

zamoose commented Sep 8, 2011

Copy link
Copy Markdown

Why not use a switch statement?

@mfields

mfields commented Sep 8, 2011

Copy link
Copy Markdown
Author

Never used one in this context before. How would it work?

@zamoose

zamoose commented Sep 8, 2011

Copy link
Copy Markdown

HEADDESK

No need for a switch. Try this:

if( ctype_xdigit($color) && ( (3 == strlen($color)) || (6 == strlen($color)) ){
return '#' . $color;
} else {
return 'transparent';
}

@zamoose

zamoose commented Sep 8, 2011

Copy link
Copy Markdown

The above should work since ctype_xdigit() will return FALSE if it detects a null (i.e., no color set).

@mfields

mfields commented Sep 8, 2011

Copy link
Copy Markdown
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