Skip to content

Instantly share code, notes, and snippets.

@mrbobbybryant
Last active February 26, 2016 16:05
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 mrbobbybryant/7141844c11a25abba5ad to your computer and use it in GitHub Desktop.
Save mrbobbybryant/7141844c11a25abba5ad to your computer and use it in GitHub Desktop.
<?php
function read_project_svg_assets( $directory ) {
$files = [];
$path = get_template_directory() . '/library/assets/*.svg';
foreach ( glob( $path ) as $filename ) {
$path_parts = pathinfo( $filename );
$files[] = $path_parts['filename'];
}
$svgs = [];
foreach ( $files as $svg_name ) {
$file = sprintf( '%s/%s/%s.svg',
esc_url( get_template_directory_uri() ),
esc_attr( $directory ),
esc_attr( $svg_name )
);
$svgs[ $svg_name ] = file_get_contents( $file );
}
return $svgs;
}
function inline_svg( $svg_name, $directory = 'assets' ) {
$svgs = get_transient( 'stored_svg_assets' );
if ( false === $svgs ) {
$svgs = read_project_svg_assets( $directory );
set_transient( 'stored_svg_assets', $svgs, 60 * 60 * 24 );
}
return $svgs[$svg_name];
}
// to use simply call inline_svg
inline_svg( 'facebook' );
//Alternately. If your svgs are not in a folder called 'assets' in the root of your project
//you can pass that location too.
inline_svg( 'facebook', 'library/svgs' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment