Skip to content

Instantly share code, notes, and snippets.

@GarySwift
Last active April 5, 2018 08:43
Show Gist options
  • Save GarySwift/3d1ce6d98f99ea919b6284ddd4220dd5 to your computer and use it in GitHub Desktop.
Save GarySwift/3d1ce6d98f99ea919b6284ddd4220dd5 to your computer and use it in GitHub Desktop.
SVG in PHP example #WordPress. It uses the PHP function 'file_get_contents' to output an inline SVG from an SVG image file.
.logo {
svg {
.cls-1{
fill:none;
stroke: get-color('primary');
}
.cls-2{
fill: get-color('primary');
}
}
}
<?php
function svg($filename, $folder='svg') {
$file = "/dist/assets/images/$folder/$filename.svg";
$file_path = get_stylesheet_directory().$file;
$file_uri = get_stylesheet_directory_uri().$file;
if (file_exists ( $file_path )) {
return file_get_contents($file_uri);
}
}
<!-- Use PHP to place inline SVG from file - expects file '/dist/assets/images/svg/logo.svg' -->
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home" class="logo"><?php
echo svg('logo);
?><span class="hide"><?php bloginfo( 'name' ); ?></span></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment