Skip to content

Instantly share code, notes, and snippets.

@matesnippets
Created March 9, 2015 15:24
Show Gist options
  • Select an option

  • Save matesnippets/e0e2b5fae944b01a93af to your computer and use it in GitHub Desktop.

Select an option

Save matesnippets/e0e2b5fae944b01a93af to your computer and use it in GitHub Desktop.
PHP, WordPress, SVG image getter, embed SVG straight to the page, no img tag needed and keeps templated nice and tidy.
/**
* Get SVG file as raw SVG, not img tag
* @param string $path Path to images direcotry
* @param string $file The filename, file extension (.svg) can be omitted
* @return string The contents of the wanted SVG file
*/
function cm_svg_get($path = 'images', $file = '')
{
// $file = $file.".svg";
$svg = ".svg";
$file = (substr($file, -strlen($svg)) === $svg) ? $file : $file.$svg;
$abs_path = get_stylesheet_directory() . '/' . $path . '/' . $file;
// Get the file into a a variable
ob_start();
include $abs_path;
$file = ob_get_contents();
ob_end_clean();
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment