Last active
May 10, 2021 19:22
-
-
Save saltnpixels/c78a2901ddc1c29809441a81eb340f22 to your computer and use it in GitHub Desktop.
Get SVG File in php
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
function get_svg_allowed_html() { | |
$allowed_html = [ | |
'svg' => [ | |
'fill' => true, | |
'height' => true, | |
'width' => true, | |
'xmlns' => true, | |
'viewbox' => true, | |
'class' => true, | |
'transform' => true, | |
'id' => true | |
], | |
'path' => [ | |
'class' => true, | |
'd' => true, | |
'id' => true, | |
'fill' => true, | |
'opacity' => true, | |
'fill-opacity' => true, | |
'style' => true, | |
'stroke' => true, | |
'stroke-width' => true, | |
'transform' => true, | |
], | |
'circle' => [ | |
'class' => true, | |
'cx' => true, | |
'cy' => true, | |
'fill' => true, | |
'r' => true, | |
], | |
'defs' => true, | |
'lineargradient' => [ | |
'class' => true, | |
'gradientunits' => true, | |
'id' => true, | |
'x1' => true, | |
'x2' => true, | |
'xlink:href' => true, | |
'y1' => true, | |
'y2' => true, | |
], | |
'stop' => [ | |
'class' => true, | |
'offset' => true, | |
'style' => true, | |
'stop-color' => true, | |
'stop-opacity' => true, | |
], | |
'rect' => [ | |
'class' => true, | |
'fill-opacity' => true, | |
'fill' => true, | |
'height' => true, | |
'rx' => true, | |
'stroke-width' => true, | |
'stroke' => true, | |
'transform' => true, | |
'width' => true, | |
'y' => true, | |
], | |
'g' => [ | |
'class' => true, | |
'clip-path' => true, | |
'fill' => true, | |
'opacity' => true, | |
], | |
'clippath' => [ | |
'class' => true, | |
'id' => true, | |
], | |
]; | |
return $allowed_html; | |
} |
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 | |
/** | |
* Custom template tags for this theme. | |
* | |
* This file is for custom template tags only and it should not contain | |
* functions that will be used for filtering or adding an action. | |
* | |
* All functions should be prefixed with UstTheme in order to prevent | |
* pollution of the global namespace and potential conflicts with functions | |
* from plugins. | |
* Example: `ust_scaffold_function()` | |
* | |
* @package UstTheme\Template_Tags | |
* | |
*/ | |
// phpcs:ignoreFile | |
if ( ! function_exists( 'ust_get_svg' ) ) : | |
/** | |
* Outputs an SVG file from the theme | |
* | |
* @param string $filename Filename of the SVG to get. | |
* @param boolean $echo Echo or return. Default false. | |
* @param array $attr Array of optional attributes. | |
* @return string HTML for the SVG. | |
*/ | |
function ust_get_svg( $filename = '', $echo = false, $attr = array() ) { | |
$default_attr = array( | |
'class' => 'svg', | |
'aria-hidden' => 'true', | |
); | |
$attrs = ''; | |
$attr = wp_parse_args( $attr, $default_attr ); | |
$attr = array_map( 'esc_attr', $attr ); | |
// Bail early if no filename is passed | |
if ( empty( $filename ) ) { | |
return; | |
} | |
if ( ! file_exists( UST_THEME_PATH . "dist/svg/{$filename}.svg" ) ) { | |
return; | |
} | |
// Get the SVG | |
$svg = file_get_contents( UST_THEME_PATH . "dist/svg/{$filename}.svg" ); // phpcs:ignore | |
foreach ( $attr as $name => $value ) { | |
$attrs .= " $name=" . '"' . $value . '"'; | |
} | |
ob_start(); | |
?> | |
<span <?php echo $attrs; // phpcs:ignore ?>> | |
<?php echo wp_kses( $svg, get_svg_allowed_html() ); ?> | |
</span> | |
<?php | |
$svg = \ob_get_clean(); | |
// Output the SVG | |
if ( $echo ) { | |
echo $svg; // phpcs:ignore | |
} | |
return $svg; | |
} | |
endif; | |
if ( ! function_exists( 'ust_the_svg' ) ) { | |
function ust_the_svg( $filename = '', $attr = array() ) { | |
ust_get_svg( $filename, true, $attr ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment