Skip to content

Instantly share code, notes, and snippets.

@timwright12
Created November 8, 2016 19:16
Show Gist options
  • Save timwright12/8b51cf3439e764f4050017b7c5ac0930 to your computer and use it in GitHub Desktop.
Save timwright12/8b51cf3439e764f4050017b7c5ac0930 to your computer and use it in GitHub Desktop.
/**
* Create svg images
*
* @param string $name Icon name.
* @param array $opts Optional attributes.
*
* @return string
*/
function svg_icon( $name, $opts = array() ) {
$href = get_stylesheet_directory_uri() . '/assets/icons/_defs/svg-defs.svg#icon-' . $name;
$role = empty( $opts['role'] ) ? 'image' : $opts['role'];
$height = empty( $opts['height'] ) ? '25' : $opts['height'];
$width = empty( $opts['width'] ) ? '25' : $opts['width'];
$class = empty( $opts['class'] ) ? 'icon icon-' . $name : 'icon ' . $opts['class'];
$label = ' aria-label="' . ( empty( $opts['label'] ) ? $name : $opts['label'] ) . '"';
$label = 'presentation' === $role ? '' : $label;
return sprintf( '<svg role="%s" height="%s" width="%s" class="%s"%s><use xlink:href="%s"/></svg>', esc_attr( $role ), esc_attr( $height ), esc_attr( $width ), esc_attr( $class ), esc_attr( $label ), esc_url( $href ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment