braican
2/26/2015 - 3:25 PM

Util function for including svg files

Util function for including svg files


if(! function_exists('include_svg')) :
/**
 * more advanced include_svg function, which checks for child themes
 *
 * @param $svg    (string) : the svg to include
 * @param $return (bool)   : whether to return the svg as a string or simply include the svg
 * @param $child  (bool)   : whether this svg icon lives in a child theme
 */
function include_svg( $svg, $return = false, $child = false ){

    $base = $child ? get_stylesheet_directory() : get_template_directory();

    // prd, assuming a build directory
    $svg_path = "$base/svg/build/$svg.svg";

    // dev
    // $svg_path = "$base/svg/$svg.svg";

    if(!file_exists($svg_path)){
        return false;
    }
    
    if($return){
        return file_get_contents($svg_path);
    }

    include( $svg_path );
}
endif;