Skip to content

Instantly share code, notes, and snippets.

@saltnpixels
Last active April 27, 2021 20:44
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 saltnpixels/3d206a35073294f73187f55675c0c80f to your computer and use it in GitHub Desktop.
Save saltnpixels/3d206a35073294f73187f55675c0c80f to your computer and use it in GitHub Desktop.
WP Get SVG icon in php from a symbol set
<?php
/**
* Icons Support with symbols to hold all icons in one file
* Simply add icons to the assets folder and run npm run icons
* This will create a symbol-defs.svg in dist with svg's
* symbol file will remove colors and fills.
*
*
* @package ignition
*/
// phpcs:ignoreFile
add_action( 'wp_footer', 'tenup_include_svg_icons', 10 );
/**
* Add SVG definitions to the footer.
*/
function tenup_include_svg_icons() {
// Define SVG sprite file.
$svg_icons = get_parent_theme_file_path( 'dist/svg/symbol-defs.svg' );
// If it exists, include it.
if ( file_exists( $svg_icons ) ) {
echo str_replace('<svg', '<svg style="display: none"', file_get_contents($svg_icons));
}
}
/**
* Return SVG markup.
* eric added aria support and better method of getting asymbol file instead
*
* @param array $args {
* Parameters needed to display an SVG.
*
* @type string $icon Required SVG icon filename.
* @type string $title Optional SVG title.
* @type string $desc Optional SVG description.
* }
* @return string SVG markup.
*/
function tenup_get_svg( $icon, $echo = true, $args = array() ) {
// Set defaults.
$defaults = array(
'icon' => $icon,
'title' => '',
'desc' => '',
'fallback' => false,
);
// Parse args.
$args = wp_parse_args( $args, $defaults );
// Set aria hidden.
$aria_hidden = ' aria-hidden="true"';
// Set ARIA.
$aria_labelledby = '';
if ( $args['title'] ) {
$aria_hidden = '';
$unique_id = uniqid();
$aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';
if ( $args['desc'] ) {
$aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
}
}
// Begin SVG markup.
$svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
// Display the title.
if ( $args['title'] ) {
$svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';
// Display the desc only if the title is already set.
if ( $args['desc'] ) {
$svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
}
}
/*
* Display the icon.
*
* The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
*
* See https://core.trac.wordpress.org/ticket/38387.
*/
$svg .= ' <use href="#' . esc_html( $args['icon'] ) . '" xlink:href="#' . esc_html( $args['icon'] ) . '"></use> ';
// Add some markup to use as a fallback for browsers that do not support SVGs.
if ( $args['fallback'] ) {
$svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
}
$svg .= '</svg>';
if ( $echo ) {
echo $svg;
return;
}
return $svg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment