I need to use file_get_contents()
in my theme to include an SVG file. As far as I know, an obvious way to do this would be the following:
file_get_contents("https://example.com/wp-content/themes/theme_name/file.svg");
However, if I do that, my server has to call itself and chokes up if there’s many visitors at the same time. When I edit the code in the following way, it gets much better, as the server fetches the file locally, without calling itself from outside:
file_get_contents("wp-content/themes/theme_name/file.svg");
There is a function that returns the path to the theme directory, get_template_directory_uri()
. But it also returns the hostname — https://example.com
— which I don’t need. I guess I could use WP constants listed in the WP Codex, but it specifically says “these [the constants] should not be used directly by plugins or themes”.
Is there a similar function that doesn’t include the hostname in what it returns?
file_get_contents
used on a URL won't work on many servers due to security protections, and the WP Codex was deprecated in favor of the developer hub and code reference several years ago at developer.wordpress.org