Last active
February 26, 2016 16:05
-
-
Save mrbobbybryant/7141844c11a25abba5ad to your computer and use it in GitHub Desktop.
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 | |
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