Skip to content

Instantly share code, notes, and snippets.

@tiborp
Created December 1, 2016 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiborp/c0a7ff4647fa3ac072e0804051e1fbe9 to your computer and use it in GitHub Desktop.
Save tiborp/c0a7ff4647fa3ac072e0804051e1fbe9 to your computer and use it in GitHub Desktop.
Nifty inline SVG's
<?php
/**
* Return SVG markup.
*
* @param array $args {
* Parameters needed to display an SVG.
*
* @param string $icon Required. Use the icon filename, e.g. "facebook-square".
* @param string $title Optional. SVG title, e.g. "Facebook".
* @param string $desc Optional. SVG description, e.g. "Share this post on Facebook".
* }
* @return string SVG markup.
*/
function prefix_get_svg( $args = array() ) {
// Make sure $args are an array.
if ( empty( $args ) ) {
return esc_html__( 'Please define default parameters in the form of an array.', 'text-domain' );
}
// YUNO define an icon?
if ( false === array_key_exists( 'icon', $args ) ) {
return esc_html__( 'Please define an SVG icon filename.', 'text-domain' );
}
// Set defaults.
$defaults = array(
'icon' => '',
'title' => '',
'desc' => ''
);
// Parse args.
$args = wp_parse_args( $args, $defaults );
// Figure out which title to use.
$title = ( $args['title'] ) ? $args['title'] : $args['icon'];
// Begin SVG markup
$svg = '<svg class="icon icon-' . esc_html( $args['icon'] ) . '" aria-hidden="true">';
// Add title markup.
$svg .= '<title>' . esc_html( $title ) . '</title>';
// If there is a description, display it.
if ( $args['desc'] ) {
$svg .= '<desc>' . esc_html( $args['desc'] ) . '</desc>';
}
$svg .= '<use xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use>';
$svg .= '</svg>';
return $svg;
}
/**
* Display an SVG.
*
* @param array $args Parameters needed to display an SVG.
*/
function prefix_do_svg( $args = array() ) {
echo prefix_get_svg( $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment