Created
January 29, 2019 06:05
-
-
Save cameronjonesweb/952d595b50a3db0b3a9cef8d37fd658a to your computer and use it in GitHub Desktop.
Generate a style tag based on an associative array of styles
This file contains 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 | |
$styles = [ | |
'color' => '#f00', | |
'background' => '#fff', | |
]; | |
cameronjonesweb_generate_style_attribute( $styles ); |
This file contains 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 | |
/** | |
* Generates a style tag based off an associative array of styles | |
* | |
* @param array $styles | |
*/ | |
function cameronjonesweb_generate_style_attribute( $styles ) { | |
return ! empty( $styles ) ? sprintf( 'style="%1$s"', implode( array_map( function( $key, $value ) { | |
return $key . ':' . $value . ';'; | |
}, array_keys( $styles ), $styles ) ) ) : ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment