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

zamoose commented Sep 8, 2011

Why not use a switch statement?

@mfields
Copy link
Author

mfields commented Sep 8, 2011

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

@zamoose
Copy link

zamoose commented Sep 8, 2011

HEADDESK

No need for a switch. Try this:

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

@zamoose
Copy link

zamoose commented Sep 8, 2011

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

@mfields
Copy link
Author

mfields commented Sep 8, 2011

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