To display an SVG icon from a folder in your WordPress theme using get_theme_file_path
, you can do the following:
$name = '360-degrees.svg';
$icon_path = get_theme_file_path('/assets/img/icons/svg/' . $name);
if (file_exists($icon_path)) {
echo file_get_contents($icon_path);
}
Explanation:
get_theme_file_path()
— This function returns the full path to a file in your theme. We pass the path to the SVG file relative to the theme’s root.file_exists()
— Checks if the file exists at the specified path to avoid errors if the file is missing.file_get_contents()
— Reads the contents of the SVG file and outputs it.
This code will embed the SVG content directly into the HTML of your page, allowing it to be used as a vector graphic.
Ensure that your SVG file is available at the specified path and that your server supports reading such files.